I have a mutable array of objects that each have multiple properties that can be shown in a detail view.
I have set up the ability to search by a property and it works fine. It shows the filtered array according to the characters searched, but when I swipe to delete within that filtered array (have not yet canceled out of search bar), it deletes the row from the filtered table and the object from the filtered array according to the index path.
I use that same index path to delete the row from the main table and the corresponding object (all within commitEditingStyle
), but as you can probably see the indexPath.row
for a certain object on the filtered mutable array will not necessarily correspond with the same object using the same indexPath
on the main mutable array. (For example, if the main array is Bob, Jamie, Sarah, Tom, and I search for "To", Tom will show but at index 0 so it would delete Bob in the main array when I try to delete Tom.)
I would like for the user to be able to delete items from search contents and the same item and its properties be deleted from both arrays and tables. Normal deletion straight from the main table view and array works fine. Here is the code:
- (void)tableView:(UITableView *)tableView commitEditingStyle:
(UITableViewCellEditingStyle)editingStyle forRowAtIndexPath:
(NSIndexPath *)indexPath {
if (editingStyle == UITableViewCellEditingStyleDelete) {
if (isFiltered == YES) {
// Then we are in filtered search results, delete from both arrays and table views
[self.personArray removeObjectAtIndex:indexPath.row]
[self.filteredArray removeObjectAtIndex:indexPath.row]
[tableView deleteRowsAtIndexPaths:@[indexPath] withRowAnimation:UITableViewRowAnimationFade];
} else { // normal table view, just remove at master array and table view
[self.personArray removeObjectAtIndex:indexPath.row]
[tableView deleteRowsAtIndexPaths:@[indexPath] withRowAnimation:UITableViewRowAnimationFade];
}
}
}
You can probably see how the indexPath.row
wouldn't match up to the same items in each array.