Welcome to the Treehouse Community

Want to collaborate on code errors? Have bugs you need feedback on? Looking for an extra set of eyes on your latest project? Get support with fellow developers, designers, and programmers of all backgrounds and skill levels here with the Treehouse Community! While you're at it, check out some resources Treehouse students have shared here.

Looking to learn something new?

Treehouse offers a seven day free trial for new students. Get access to thousands of hours of content and join thousands of Treehouse students and alumni in the community today.

Start your free trial

iOS

Setting Buttons in Prototype cell of Table View

I'm working on a project that restricts user access to the app by providing approved users with an access code. On the main screen, users are asked for some basic information, which is then submitted to administrators who then approve access and send an access code. Users whose status level is set to "administrator" have the option of navigating to a table view screen that lists all the users. Each requested user is listed in a cell along with two buttons:"Accept" and "Deny".

So for instance, if I am looking at a cell that has "John Smith" with a userID of "12345", there is also an "Accept" and "Deny" button in the cell. Ultimately I want to be able to press the "Accept" button, for instance, and execute the code that will email John Smith an access code.

The problem I'm running into here, is that I can't figure out how to get the buttons in the cell to gather information based on the indexpath.row where it is nested.

I have attempted to do this programmatically both within the Button's IBAction by setting tags, and also in the UITableViewCell, without success. Each time, no matter which row's button I press, my NSLog returns "0" for the clicked button path. Below is the code as it is currently written, which is kind of all over the place now because of all the various methods I have attempted.

Does anyone have an idea how I might go about dealing with this issue?

- (UITableViewCell *)tableView:(UITableView *)tableView cellForRowAtIndexPath:(NSIndexPath *)indexPath
{
    static NSString *CellIdentifier = @"Cell";
    UITableViewCell *cell = [tableView dequeueReusableCellWithIdentifier:CellIdentifier forIndexPath:indexPath];

    HeaderData *obj = [self.pendingList objectAtIndex:indexPath.row];

    UILabel *userLabel = (UILabel *)[cell viewWithTag:1];
    UILabel *committeeLabel = (UILabel *)[cell viewWithTag:2];
    UILabel *orgLabel = (UILabel *)[cell viewWithTag:3];
    UIButton *approvedButton = (UIButton *)[cell viewWithTag:10];
    UIButton *deniedButton = (UIButton *)[cell viewWithTag:11];
    userLabel.text = [[obj.firstName stringByAppendingString:@" "] stringByAppendingString:obj.lastName];
    committeeLabel.text = obj.committee;
    orgLabel.text = obj.org;


    return cell;

}



- (IBAction)approvedButton:(id)sender {
    UITableViewCell *clickedCell = (UITableViewCell *)[[sender superview] superview];
    NSIndexPath *clickedButtonPath = [self.tableView indexPathForCell:clickedCell];
    NSLog(@"%@", clickedButtonPath);
}

- (IBAction)deniedButton:(id)sender {
}

2 Answers

after experimenting a bit, it looks like the cell is 3 layers up in the hierarchy, not 2 like you have, and not 1 like I thought. The superview of the button is the cell content view, and the superview of the content view is the cell scroll view, and the super view of the scroll view is the cell. so try using

UITableViewCell *cell = (UITableViewCell *)[[[sender superview] superview] superview];

I logged the class of the cell object to determine what was what

NSLog(@"%@", [cell class]);

So try that and see if your code will work now

Great! That did the trick, and I learned something about superviews and hierarchies in the process.

Thanks so much! This had me stumped for several hours.

Back to the grind...

hmm interesting. this line here

UITableViewCell *clickedCell = (UITableViewCell *)[[sender superview] superview]

are you sure the cell is the superview of the superview of the button? the way you have added your button to the cell using tags makes me think its only one level above it, not 2.

maybe try using

UITableViewCell *clickedCell =  (UITableViewCell *)[[sender superview]

or experimenting with the view hierarchy a bit more.

I think the best way to do this would be to use a custom cell class though, which might be a little more work than you are looking for