【发布时间】:2011-11-14 17:44:51
【问题描述】:
我有一个表格视图,其中一些相同的单词在不同的节标题和一个搜索字段下。 当您搜索时,您会得到重复的结果,我想从我的搜索结果中过滤掉这些重复的单词。
任何想法如何做到这一点?
【问题讨论】:
-
您正在决定搜索期间显示的内容。如果您不想要它们,为什么要添加重复项。我不明白这个问题。
标签: iphone uitableview search xcode4
我有一个表格视图,其中一些相同的单词在不同的节标题和一个搜索字段下。 当您搜索时,您会得到重复的结果,我想从我的搜索结果中过滤掉这些重复的单词。
任何想法如何做到这一点?
【问题讨论】:
标签: iphone uitableview search xcode4
这是我用来做确切事情的一段代码。您需要比较每个对象的唯一字符串以识别重复项。在这个例子中,我用“myObject.uniqueID”来做。创建一个没有重复的数组,然后[tableView reloadData];
- (void) removeDuplicates{
NSMutableDictionary * d = [[[NSMutableDictionary alloc] initWithCapacity:0] autorelease];
NSMutableArray * noDuplicatesArray = [[[NSMutableArray alloc] initWithCapacity:0] autorelease];
Object* myObject; // whatever your object is
NSString * key;
NSString * value;
for ( int i=0; i<[duplicatesArray count]; i++ ){
myObject = [duplicatesArray objectAtIndex:i];
key = myObject.uniqueID; // whatever makes this object unique (like a stock number, or ID)
value = [d objectForKey:key];
if ( value == nil ){
[d setObject:key forKey:key];
[noDuplicatesArray addObject:deal];
}
}
// now your noDuplicatesArray will have only unique entries.
// reload your tableview using the noDuplicatesArray
}
【讨论】: