I have a weird problem going on here. I have a TableView and I assign the indexPath.row to the tag of a UIButton so I can pass that tag as a parameter on a segue and give an object to the next view controller. Everything works hunky dory until I go past 6 cells in the TableView, when pressing the button passes the wrong object from the array. I decided to put in some NSLogs to see what was happening and I find the results quite bizarre. The assignment buttonForApplyingTag.tag = indexPath.row; happens just after the NSLogs and is the last statement before return cell;. Since the actual value of indexPath.row is correct, the objects from the array are getting loaded properly. It’s just the buttons that are causing problems. Here is my output:
2012-11-28 19:01:57.596 IndexPath.row: 0
2012-11-28 19:01:57.596 IndexPath.Row from Tag: 0
2012-11-28 19:01:57.597 SearchResults Count: 100
2012-11-28 19:01:57.598 IndexPath.row: 1
2012-11-28 19:01:57.598 IndexPath.Row from Tag: 1
2012-11-28 19:01:57.598 SearchResults Count: 100
2012-11-28 19:01:57.599 IndexPath.row: 2
2012-11-28 19:01:57.600 IndexPath.Row from Tag: 2
2012-11-28 19:01:57.600 SearchResults Count: 100
2012-11-28 19:01:57.601 IndexPath.row: 3
2012-11-28 19:01:57.601 IndexPath.Row from Tag: 3
2012-11-28 19:01:57.602 SearchResults Count: 100
2012-11-28 19:01:57.602 IndexPath.row: 4
2012-11-28 19:01:57.603 IndexPath.Row from Tag: 4
2012-11-28 19:01:57.603 SearchResults Count: 100
2012-11-28 19:01:57.604 IndexPath.row: 5
2012-11-28 19:01:57.605 IndexPath.Row from Tag: 5
2012-11-28 19:01:57.605 SearchResults Count: 100
2012-11-28 19:02:02.004 IndexPath.row: 6
2012-11-28 19:02:02.004 IndexPath.Row from Tag: 6
2012-11-28 19:02:02.005 SearchResults Count: 100
2012-11-28 19:02:03.993 IndexPath.row: 7
2012-11-28 19:02:03.993 IndexPath.Row from Tag: 0
2012-11-28 19:02:03.993 SearchResults Count: 100
2012-11-28 19:02:17.846 IndexPath.row: 8
2012-11-28 19:02:17.846 IndexPath.Row from Tag: 0
2012-11-28 19:02:17.846 SearchResults Count: 100
2012-11-28 19:02:18.482 IndexPath.row: 9
2012-11-28 19:02:18.482 IndexPath.Row from Tag: 0
2012-11-28 19:02:18.482 SearchResults Count: 100
Sorry for the excessive amount of lines, but you get the idea. This trend continues all the way down to 100, meaning that the buttons are not receiving the correct tag. Even weirder is the fact that when the button is clicked on the cells with a false indexPath.row tag, another tag is apparently made up and I get a low value tag. Furthermore, when the cell 6 has partially appeared at the bottom of the TableView, even though it’s clear the cell has actually loaded, the NSLogs haven’t been posted to the debugger, and therefore the button fails and returns a tag of 5. So there are actually 3 issues with this way of doing things.
Really, what I want to ask is why is this strange behaviour happening, and how can I fix it? And secondly, since this just appears to be a disaster, is there a better way of passing down the indexPath.row value so I can get an object from my array when the button is clicked?
Thanks for any help.
Regards,
Mike
P.S. I’m a complete amateur so if you could explain it in Lehmann’s terms, I’d appreciate it.
EDIT: Here’s the code where I actually assign the tag to the button, as requested.
UIButton *buttonForApplyingTag = (UIButton *)[cell viewWithTag:1004];
NSLog(@"IndexPath.row: %d", indexPath.row);
NSLog(@"IndexPath.Row from Tag: %d", buttonForApplyingTag.tag);
NSLog(@"SearchResults Count: %d", [searchResults count]);
buttonForApplyingTag.tag = indexPath.row;
return cell;
EDIT 2: Here’s the entire cellForRowAtIndexPath code:
if (isLoading) {
return [tableView dequeueReusableCellWithIdentifier:LoadingCellIdentifier];
} else if ([searchResults count] == 0) {
return [tableView dequeueReusableCellWithIdentifier:NothingFoundCellIdentifier];
} else {
UITableViewCell *cell = [tableView dequeueReusableCellWithIdentifier:SearchResultCellIdentifier];
[tableView setSeparatorStyle:UITableViewCellSeparatorStyleSingleLine];
[tableView setSeparatorColor:[UIColor grayColor]];
/* Then we do a load of assignments for labels in the cell, then we reach the tagging code. */
SearchResultCellIdentifier comes from this: static NSString *const SearchResultCellIdentifier = @"SearchResultCellProto";
It looks like what you want to do is have buttons in cells, and then know which cell’s button was pressed when the button action occurs. Using tags is a reasonable idea, but tough to implement because the tag needs to change when the cell gets reused (and ends up at a different index path).
Here’s a better approach: Add the buttons using a constant tag (or no tags), then on the click action do this…