I've encountered an error when running with release configuration, which seems to be the premature release of local variable tmp
.
*** Terminating app due to uncaught exception 'NSInternalInconsistencyException', reason: 'Invalid index path for use with UITableView. Index paths passed to table view must contain exactly two indices specifying the section and row. Please use the category on NSIndexPath in UITableView.h if possible.'
Related code:
@property (nonatomic, strong) NSIndexPath *selectedCellIndexPath;
...
- (void)tableView:(UITableView *)tableView didSelectRowAtIndexPath:(NSIndexPath *)indexPath
{
if (_selectedCellIndexPath != nil && [_selectedCellIndexPath isEqual:indexPath]) {
self.selectedCellIndexPath = nil;
[tableView reloadRowsAtIndexPaths:@[indexPath] withRowAnimation:UITableViewRowAnimationFade];
} else if (_selectedCellIndexPath != nil && ![_selectedCellIndexPath isEqual:indexPath]) {
//--- problematic code
NSIndexPath *tmp = _selectedCellIndexPath;
self.selectedCellIndexPath = indexPath;
[tableView reloadRowsAtIndexPaths:@[tmp, _selectedCellIndexPath] withRowAnimation:UITableViewRowAnimationFade];
//--- problematic code
} else {
self.selectedCellIndexPath = indexPath;
[tableView reloadRowsAtIndexPaths:[NSArray arrayWithObject:indexPath] withRowAnimation:UITableViewRowAnimationFade];
}
}
I had an impression that local variable tmp
should have strong reference here or I am not right?
Btw, changing code to
NSIndexPath *tmp = self.selectedCellIndexPath;
Or changing
@[tmp, _selectedCellIndexPath]
to [NSArray arrayWithObjects:tmp,_selectedCellIndexPath,nil]
fixes the problem.
What would be the explanation what goes wrong here?