我正在实现一个搜索字段,该字段根据用户输入的文本过滤UITableView
TableView由一个数组构建,该数组包含NSString(要显示和搜索的数据),并且可能包含6000+个项目
当用户开始搜索时,我正在实现-(void)searchBar:(UISearchBar *)searchBar textDidChange:(NSString *)searchText
方法
然而,我的代码是有效的,当数据数组很大时,它会非常慢,并产生非常糟糕的用户体验(我的iPhone 4s会被卡住好几秒钟)
我实现搜索的方式(在上面提到的方法中)是这样的:
NSMutableArray *discardedItems = [[NSMutableArray alloc] init]; // Items to be removed
searchResultsArray = [[NSMutableArray alloc] initWithArray:containerArray]; // The array that holds all the data
// Search for matching results
for (int i=0; i<[searchResultsArray count]; i++) {
NSString *data = [[containerArray objectAtIndex:i] lowercaseString];
NSRange r = [data rangeOfString:searchText];
if (r.location == NSNotFound) {
// Mark the items to be removed
[discardedItems addObject:[searchResultsArray objectAtIndex:i]];
}
}
// update the display array
[searchResultsArray removeObjectsInArray:discardedItems];
[myTableView reloadData];
我不认为在一个有几千个项目的数组上循环会引起任何问题
任何建议都将不胜感激
更新我刚刚意识到,花大部分时间的是:
[searchResultsArray removeObjectsInArray:discardedItems];
尝试快速枚举方式,我的代码片段:
- (void)searchBar:(UISearchBar*)searchBar textDidChange:(NSString*)text
{
if(text.length == 0)
{
self.isFiltered = NO;
}
else
{
self.isFiltered = YES;
self.searchArray = [NSMutableArray arrayWithCapacity:self.places.count];
for (PTGPlace* place in self.places)
{
NSRange nameRange = [place.name rangeOfString:text options:NSCaseInsensitiveSearch];
if(nameRange.location != NSNotFound)
{
[self.searchArray addObject:place];
}
}
}
[self.tableView reloadData];
}
- (NSInteger)tableView:(UITableView *)tableView numberOfRowsInSection:(NSInteger)section
{
if(self.isFiltered)
return self.searchArray.count;
else
return self.places.count;
}
cellForRowAtIndexPath:
PTGPlace *place = nil;
if(self.isFiltered)
place = [self.searchArray objectAtIndex:indexPath.row];
else
place = [self.places objectAtIndex:indexPath.row];
// Configure the cell...
cell.textLabel.text = place.name;
cell.detailTextLabel.text = [place subtitle];
试试这个:
对于前三个位置,创建26个索引集,每个索引集表示具有该字母的项的数组索引(小写)。也就是说,假设idx=100的条目以"formula"开头。在第一个位置表示"f"的索引集将包含索引"100"。第二个字符"o"的索引集将包含索引100,第三个字符"r"的索引集中将包含100。
当用户键入字符"f"时,您将立即获得以"f"开头的所有数组项的索引集(并且可以快速创建主数组的子集)。当接下来键入"o"时,您可以在第一个匹配项和第二个匹配项中找到索引的交集。第三个也是如此。现在,为前三个索引匹配的主数组创建一个子数组——您只需使用索引集即可。
使用这个大大减少的数组,您现在可以像原来一样进行蛮力匹配。