Code updated to working version. Thanks again for the help :)
Hey guys. I have a UITableViewController set up to use a custom cell loaded from a nib. Here's my cellForRowAtIndexPath:
// Customize the appearance of table view cells.
- (UITableViewCell *)tableView:(UITableView *)tableView cellForRowAtIndexPath:(NSIndexPath *)indexPath {
NSString *cellIdentifier = @"PeopleFilterTableViewCell";
UITableViewCell *cell = [tableView dequeueReusableCellWithIdentifier:cellIdentifier];
if (cell == nil) {
[[NSBundle mainBundle] loadNibNamed:@"PeopleFilterTableViewCell" owner:self options:nil];
cell = peopleFilterTableViewCell;
self.peopleFilterTableViewCell = nil;
}
cell.selectionStyle = UITableViewCellSelectionStyleNone;
PeopleFilterTableViewCell* tableViewCell = (PeopleFilterTableViewCell *) cell;
/* Set direct button name */
Person* personAtRow = [directsToShow objectAtIndex:indexPath.row];
[tableViewCell.directButton setTitle:personAtRow.name forState:UIControlStateNormal];
/* Set direct head count */
tableViewCell.headcountLabel.text = [NSString stringWithFormat:@"%d", personAtRow.totalHeadCount];
UIImage* unselectedImage = [UIImage imageNamed:@"filterButton.png"];
UIImage* selectedImage = [UIImage imageNamed:@"filterButtonClosed.png"];
UIButton* newFilterButton = [UIButton buttonWithType:UIButtonTypeRoundedRect];
/* Set filter button image */
if(personAtRow.filtered){
[newFilterButton setSelected:YES];
} else {
[newFilterButton setSelected:NO];
}
tableViewCell.filterButton = newFilterButton;
return cell;
}
This seems to work fine for me, but one issue has come up with the code after the /* set filter button image */ comment.
The filter button is a UIButton in my custom cell nib that is supposed to reflect the state of a model array containing 'Person' objects, which have a field that can be toggled to represent whether they are being filtered or not.
The way that I allow a user to update this model object is through a custom delegate method on my top level controller, which, whenever the user clicks the filter button, updates the model and the state of the button, and additionally updates a mapViewController with some data to show based on the state of the model:
- (void)updateViews:(id)sender {
UIImage* unselectedImage = [UIImage imageNamed:@"filterButton.png"];
UIImage* selectedImage = [UIImage imageNamed:@"filterButtonClosed.png"];
int row = [self rowOfCellSubView:sender];
Person* personToFilter = [self.directsToBeShown objectAtIndex:row];
NSLog(@"Filtering person with corpId: %@, name: %@", personToFilter.corpId, personToFilter.name);
if (personToFilter.filtered){
//update button image
[sender setImage:unselectedImage forState:UIControlStateNormal];
[sender setSelected:NO];
//add person back.
Person* directFiltered = [self.directsToBeShown objectAtIndex:row];
directFiltered.filtered = NO;
NSLog(@"Filtering person with corpId: %@, name: %@, filtered: %d", directFiltered.corpId, directFiltered.name, directFiltered.filtered);
} else {
//update button image
[sender setImage:selectedImage forState:UIControlStateSelected];
[sender setSelected:YES];
//remove person.
personToFilter.filtered = YES;
NSLog(@"Filtering person with corpId: %@, name: %@, filtered: %d", personToFilter.corpId, personToFilter.name, personToFilter.filtered);
}
[self updateSitesToShow];
[self.mapViewController performSelectorOnMainThread:@selector(updateDisplay) withObject:nil waitUntilDone:NO];
}
My issue comes with the updating of the state for the filter button. When my app loads the tableview, everything looks fine. When I click a filter button in a certain cell row the state of the button updates correctly, and my model objects are also updating correctly since I see the expected behavior from the mapView which I'm ultimately updating.
However, the issue is that when I click on the filterButton in one cell row and then scroll down a few rows, I notice another filter button in a different cell now has the same state as the one I clicked a few rows above. If I scroll back up again, the original button I clicked 'on' now seems to be 'off' but the row below it now appears 'on'. Of course all this is affecting is the actual display of the buttons. The actual state of the buttons is consistent and working correctly.
I know this issue must have something to do with the fact that my cells are being reused, and I'm guessing somehow the same buttons are being referenced for different cell rows. I'm at a loss as to how this is happening though, since I'm creating a new filter button for each cell, whether the cell is reused or not, and resetting the filterButton property of the cell to be the newly created object. Notice for example that the text of the headCount property, which is a UILabel also defined in the cell nib, is also being reassigned to a new String object for each cell, and it is displaying correctly for each row.
I've been struggling with this problem for a few days now. Any help or suggestions at all would be really appreciated.