I have a tableView who's datasource is an array with 400 static items in it. In my app you can select a row and it will place a checkmark on that row. I'm keeping track of the checked item's indexPaths in another array so that the table can be reloaded later and those items will still be checked.
Since my table has a lot of values in it I've added a searchDisplayController. Start typing what your looking for and it'll filter the list down to those items. You can select rows in the searchResultsTableView and it will check them, just like in the main table with the 400 static rows. However, there's a problem:
Let's say you enter a search and narrow the list of 400 items down to the one you're looking for and you select (checkmark) it. In the full list of 400 items, the one you just searched for might be number 112 in the list, however when you did your search and filtered down to only that one item, instead of adding the indexPath of item 112 to my array that keeps track, it entered the indexPath of item 0 because it was the only item showing in the filtered list. So when you cancel out of search and go back to the main list instead of their being a checkmark on item 112, there's a checkmark on item 0.
So I'm looking for a way to keep my filtered array in sync with my main tableView datasource.
The relevant bit of my didSelectRowAtIndexPath method:
if (tableView == self.searchDisplayController.searchResultsTableView) {
cell.textLabel.text = [filteredattributesArray objectAtIndex:[indexPath row]];
if(cell.accessoryType == UITableViewCellAccessoryNone){
cell.accessoryType = UITableViewCellAccessoryCheckmark;
[selectedItemRows addObject:indexPath]; //Add the index path of checked cell into array to keep track
[tableView reloadData];
} else {
if(cell.accessoryType == UITableViewCellAccessoryCheckmark) {
cell.accessoryType = UITableViewCellAccessoryNone;
[selectedItemRows removeObject:indexPath]; //Remove that index path of unchecked cell from index array
[tableView reloadData];
}
}
[tableView deselectRowAtIndexPath:indexPath animated:YES]; }
}
Don't use indexPath. Set the tag value on each item in your data array and then keep a list of tags for your "checkedArray". That way you can evaluate the checked items independent of the cells or tableView structure.