在大型数组中搜索字符串需要很长时间

时间:2012-09-01 20:05:46

标签: iphone objective-c ios nsstring searchbar

我正在实施一个搜索字段,根据用户输入的文本过滤UITableView TableView是由一个包含NSStrings(要显示和搜索的数据)的数组构建的,可能包含 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];

2 个答案:

答案 0 :(得分:1)

尝试快速枚举方式,我的代码段:

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

答案 1 :(得分:0)

试试这个:

对于前三个位置,创建26个索引集,每个索引集表示具有该字母的项目的数组索引(只是小写)。也就是说,idx = 100的条目以“公式”开头。在第一个位置表示“f”的索引集将包含索引“100”。第二个字符“o”的索引集将包含索引100,第三个字符“r”的索引集将包含100个。

当用户键入字符'f'时,您立即拥有以'f'开头的所有数组项的索引集(并且可以快速创建主数组的子集)。当接下来输入'o'时,您可以在第一个匹配中找到与第二个匹配的索引的交集。第三个同上。现在制作一个前三个索引匹配的主要数组的子数组 - 你可以只使用索引集。

使用这个大幅减少的数组,您现在可以像原来那样进行强力匹配。