Buttons in UITableViewCells - Determining section and row

Discussion in 'iOS Programming' started by cstromme, Mar 16, 2010.

  1. cstromme macrumors regular

    Feb 26, 2007
    So I have a UITableView here with several sections and with several rows per section. Both the number of sections and the number of rows per section is different from each specific case the tableview is used for.

    I have two UIButtons in the tableview that both fire their separate methods.

    The problem is that I'm having trouble figuring out which section and which row the button belongs to in the method that gets fired when you push the button. After some thinking I realized I could set the row as the tag of the button (since this gets passed into the method I'm running), but that leaves the section undetermined. I then thought perhaps I could set the title of the button to the section, since I'm using an image on the button, but for some reason I can't seem to extract this in the method that gets fired.

    			UIButton *increaseButton = [UIButton buttonWithType:UIButtonTypeCustom];
    				NSString *section = [NSString stringWithFormat:@"Section: %i",(indexPath.section - 2)];
    				int line = ((indexPath.row - 1) / 2);
    				[increaseButton setTag:line];
    				[increaseButton setTitle:section
    				[increaseButton setTitleColor:[UIColor greenColor]
    				[increaseButton setImage:[UIImage imageNamed:@"bidincrease.png"]
    				[increaseButton setFrame:CGRectMake(10, 10, 30, 25)];
    				[increaseButton setUserInteractionEnabled:YES];
    				[increaseButton addTarget:self
    				[cell setAccessoryView:increaseButton];
    				UIButton *decreaseButton = [UIButton buttonWithType:UIButtonTypeCustom];
    				[decreaseButton setTag:line];
    				[decreaseButton setTitle:section
    				[decreaseButton setImage:[UIImage imageNamed:@"biddecrease.png"]
    				[decreaseButton setFrame:CGRectMake(10, 10, 30, 25)];
    				[decreaseButton setUserInteractionEnabled:YES];
    				[decreaseButton addTarget:self
    				[cell.contentView addSubview:decreaseButton];
    The increaseLine method looks like this:
    - (void)increaseLine:(UIButton *)sender
    	NSLog(@"Increasing line %@",sender);
    //	NSLog("Section: %@\nLine: %i",[sender currentTitle],[sender tag]);
    The sender here comes out as:
    With no mention of the title. Trying to run that second line that is commented out the app crashes and there's a warning in XCode that I'm passing argument 1 from an incompatible pointer type.

  2. Luke Redpath macrumors 6502a

    Nov 9, 2007
    Colchester, UK
    If you have access to the UIButton, and it is a subview of your tableview cell's content view, then you should be able to go up the view hierarchy using the superview method until you find your cell.

    You should then be able to get the index path of that cell by using the method [tableView indexPathForCell:yourcell].
  3. teek macrumors member


    Feb 12, 2008
    Use the UIButton's superview

    in your IBAction method:

    UIButton *theButton = (UIButton*)sender;

    UITableViewCell *cell = [theButton superview];

    Then to get the row/section use UITableView:indexPathForCell method
  4. PhoneyDeveloper macrumors 68040


    Sep 2, 2008
    There are several choices here.

    Even though a tag is the size of a single int you can stuff two shorts into it. As long as your row and section count will never be greater than MAX_UNSIGNED_SHORT then this will be simple and will work. Just set the tag for the buttons in your cellForRowAtIndexPath so it contains both row and section.

    You can subclass UIButton and add a property for an indexPath or add properties for row and section.

Share This Page