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)];