我有一个为字幕单元格配置的 UITableView
。当我向 TableView
添加一个单元格时,我有两个数组。一个用于副标题,另一个用于主标签。我还有一个 NSMutableDictionary
,这样当我将单元格添加到表中时,它会记录一个对象和一个键。我的问题是当我想从 UITableView
中删除单元格时,我使用了这个方法:
- (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.
}
}
这非常适合从关联数组中删除对象,但我也想从我创建的 MutableDictionary
中删除对象。我以为您会使用类似于我在数组中使用的东西。
[myArray removeObjectAtIndex:indexPath.row];
但是我不会用
[myDictionary removeObjectForKey:indexPath.row];
有人知道我会怎么写吗?
最佳答案
由于 indexPath.row
是原始类型,它不能用作 NSDictionary
中的键:您需要将其包装在 NSNumber
中为了做到这一点。
[myDictionary removeObjectForKey:[NSNumber numberWithInt:indexPath.row]];
仅当您使用 NSNumber
包装的整数作为 NSMutableDictionary
中的键时,这才有效。通常,由于 NSDictionary
是一个关联容器,您需要在调用 removeObjectForKey
时使用与调用 setObject:forKey 时相同的键:
。
关于objective-c - 从索引处的 NSMutableDictionary 中删除对象,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/13557489/