Warning: file_get_contents(/data/phpspider/zhask/data//catemap/5/objective-c/25.json): failed to open stream: No such file or directory in /data/phpspider/zhask/libs/function.php on line 167

Warning: Invalid argument supplied for foreach() in /data/phpspider/zhask/libs/tag.function.php on line 1116

Notice: Undefined index: in /data/phpspider/zhask/libs/function.php on line 180

Warning: array_chunk() expects parameter 1 to be array, null given in /data/phpspider/zhask/libs/function.php on line 181
Objective c 从索引处的NSMutableDictionary中删除对象_Objective C_Ios_Xcode - Fatal编程技术网

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

Objective c 从索引处的NSMutableDictionary中删除对象,objective-c,ios,xcode,Objective C,Ios,Xcode,我有一个为字幕单元格配置的UITableView。当我向表视图添加单元格时,我有两个数组。一个用于字幕,另一个用于主标签。我还有一个NSMutableDictionary,这样当我将单元格添加到表中时,它会记录一个对象和一个键。我的问题是,当我想从UITableView中删除单元格时,我使用以下方法: - (void)tableView:(UITableView *)tableView commitEditingStyle:(UITableViewCellEditingStyle)editing

我有一个为字幕单元格配置的
UITableView
。当我向
表视图添加单元格时,我有两个数组。一个用于字幕,另一个用于主标签。我还有一个
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];

有人知道我会怎么写吗?

因为
indexath.row
是一种基本类型,它不能用作
NSDictionary
中的键:您需要将它包装在
NSNumber
中才能这样做

[myDictionary removeObjectForKey:[NSNumber numberWithInt:indexPath.row]];

只有在
NSMutableDictionary
中使用
NSNumber
包装的整数作为键时,这才有效。通常,由于
NSDictionary
是一个关联容器,因此在调用
removeObjectForKey
时需要使用与调用
setObject:forKey:
时相同的键。使用此代码从NSMutableDictionary中删除数据

//First get all the keys of dictionary into one array
 NSArray *sectionsArray = [mutaleDictionary allKeys];
//Get all the data of tapped section into one array by using indexpath.section
 NSMutableArray *objectsAtSection = [mutaleDictionary valueForKey:[sectionsArray objectAtIndex:indexPath.section]];
//remove the particular object by using indexPath.row from the array
 [objectsAtSection removeObjectAtIndex:indexPath.row]; 

字典中没有索引。字典使用键。@HotLicks表示字典中没有索引。在字典中添加条目时使用什么键?@HotLicks,我相信你的意思是“字典中没有索引”。正如rdelmar所建议的,你可以使用插入词条时使用的同一个键进行删除。但他尚未说明如何将词条放入词典。单元格的副标题与词典的键相同。如果这有帮助的话。@Craig您的
removeObjectForKey
参数需要与您使用的
setObject:forKey:
键匹配,而且它显然不是一个整数值,这就是
indexPath.row
的含义。@dasblinkenlight@Craig现在您可以使用带有叮当声的文本,例如
[myDictionary removeObjectForKey:@(indexPath.row)];