我的很多代碼都基於Apple的TableSearch示例,但是我的應用程序包含需要搜索的35,000個單元,而不是示例中的少數幾個。由於UISearchDisplayController相對較新,因此沒有太多有關UISearchDisplayController的文檔。我使用的代碼如下:調整iPhone TableSearch算法以防止UI延遲
- (void)filterContentForSearchText:(NSString*)searchText {
/*
Update the filtered array based on the search text and scope.
*/
[self.filteredListContent removeAllObjects]; // First clear the filtered array.
/*
Search the main list for products whose type matches the scope (if selected) and whose name matches searchText; add items that match to the filtered array.
*/
AppDelegate *appDelegate = (AppDelegate *)[[UIApplication sharedApplication] delegate];
for (Entry *entry in appDelegate.entries)
{
if (appDelegate.searchEnglish == NO) {
NSComparisonResult result = [entry.gurmukhiEntry compare:searchText options:(NSCaseInsensitiveSearch|NSDiacriticInsensitiveSearch) range:NSMakeRange(0, [searchText length])];
if (result == NSOrderedSame)
{
[self.filteredListContent addObject:entry];
}
}
else {
NSRange range = [entry.englishEntry rangeOfString:searchText options:NSCaseInsensitiveSearch];
if(range.location != NSNotFound)
{
[self.filteredListContent addObject:entry];
}
}
}}
- (BOOL)searchDisplayController:(UISearchDisplayController *)controller shouldReloadTableForSearchString:(NSString *)searchString {
[self filterContentForSearchText:searchString];
[self.view bringSubviewToFront:keyboardView];
// Return YES to cause the search result table view to be reloaded.
return YES;}
我的問題是有一點的延遲按下鍵盤上的每個按鈕後。這成爲一個可用性問題,因爲用戶在輸入每個字符後必須等待App搜索數組以匹配結果。如何調整此代碼,以便用戶可以連續輸入而不會有任何延遲。在這種情況下,延遲數據重新加載所需的時間是可以的,但在鍵入時不應阻塞鍵盤。
不正是我期待的...我已經看到它在用戶鍵入之前完成,有延遲加載表格,但它與可以自由輸入大型數據庫的鍵盤分開。 – Kulpreet 2009-07-29 07:41:00