I have a UITableView that is configured for a subtitle cell. When I add a cell to the TableView I have two arrays. One for the subtitle and the other for the main label. I also have an NSMutableDictionary so that when I add the cell to the table it records an object and a key. My problem is when I want to delete the cell from the UITableView I use this method:
- (void)tableView:(UITableView *)tableView commitEditingStyle:(UITableViewCellEditingStyle)editingStyle forRowAtIndexPath:(NSIndexPath *)indexPath
{
if (editingStyle == UITableViewCellEditingStyleDelete) {
[myArray removeObjectAtIndex:indexPath.row];
[numberArray removeObjectAtIndex:indexPath.row];
[tableView deleteRowsAtIndexPaths:@[indexPath] withRowAnimation:UITableViewRowAnimationFade];
} else if (editingStyle == UITableViewCellEditingStyleInsert) {
// Create a new instance of the appropriate class, insert it into the array, and add a new row to the table view.
}
}
This works great for removing the objects from their associated arrays, but I also want to remove the objects from the MutableDictionary I made. I thought that you would use somethings similar to what I used with the arrays.
[myArray removeObjectAtIndex:indexPath.row];
But I can’t use
[myDictionary removeObjectForKey:indexPath.row];
Does anyone know how I would write this?
Since
indexPath.rowis a primitive type, it cannot be used as a key inNSDictionary: you need to wrap it inNSNumberin order to do so.This would work only if you use
NSNumber-wrapped integers as keys in yourNSMutableDictionary. In general, sinceNSDictionaryis an associative container, you need to use the same key in the calls ofremoveObjectForKeyas you used in the calls ofsetObject:forKey:.