objective-c - 从索引处的 NSMutableDictionary 中删除对象

标签 objective-c ios xcode

我有一个为字幕单元格配置的 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/

相关文章:

ios - AVFoundation 自定义相机导致崩溃/内存压力?

iOS SDK AVPlayer - 如何检测音频流是否可搜索?

xcode - 使用 Swift,我似乎只有在使用 arc4random 时才会出现此错误。为什么,我该如何解决?

iphone - 一次可以运行多少个 SQL 查询?

ios - 为我的变量编写自定义 Setter - Swift

iphone - 动态地全局将(null)替换为'0'

c++ - 带有 boost : linker(Id) Warning about visibility settings 的 xcode

ios - 如何上传这张照片?

android - 为什么 TYPE_LINEAR_ACCELERATION 在设备静止时产生非零值?

iphone - 如何更改我的 iPhone 应用程序二进制文件名