目前我正在编写一个带有 tableView 的应用程序,类似于 iPhone 通讯录应用程序中的应用程序。
一切正常(部分,右侧显示标题的栏,单元格已配置......),在搜索栏旁边。如果 tableView 的数据是从数组加载的,我很熟悉如何执行此操作(搜索),但我的情况是它是从位于 NSDictionary 中的数组加载的。
字典看起来像
Key = "A" >> Value = "array = apple, animal, alphabet, abc ..."
Key = "B" >> Value = "array = bat, ball, banana ..."
如何删除与搜索词匹配的所有字符串(从字典的所有数组中)?
提前致谢:)
最佳答案
你可以这样做
NSMutableDictionary *newItems = [NSMutableDictionary dictionary];
for (NSString *key in oldItems) {
NSMutableArray *newArray = [NSMutableArray array];
for (NSString *item in [oldItems objectForKey:key]) {
if ([item rangeOfString:searchTerm].location != NSNotFound) {
[newArray addObject:item];
}
}
if ([newArray count]) {
[newItems setObject:newArray forKey:key];
}
}
[oldItems release];
oldItems = [newItems retain];
我不知道这是否是最好的方法,或者即使它足够快,但请告诉我这是否适合您。
关于iphone - 从字典中的数组中删除与搜索词匹配的所有值(字符串)?,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/6976515/