2009-07-29 22 views
0

我的很多代碼都基於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搜索數組以匹配結果。如何調整此代碼,以便用戶可以連續輸入而不會有任何延遲。在這種情況下,延遲數據重新加載所需的時間是可以的,但在鍵入時不應阻塞鍵盤。

回答

0

更新:到acccomplish而打字searchching沒有「鎖定」的UI

的一種方法,是使用線程。

所以,你可以調用與此方法進行排序的方法:

- (void)performSelectorInBackground:(SEL)aSelector withObject:(id)arg 

這將保證它的主線程的,允許UI更新。

您將不得不在後臺線程上創建並刪除自己的自動釋放池。

然而,當你想更新,你將不得不消息回主線程表(所有UI更新必須在主線程):

- (void)performSelectorOnMainThread:(SEL)aSelector withObject:(id)arg waitUntilDone:(BOOL)wait 

你也可以得到多一點的控制通過使用NSOperation/NSOperationQueue或NSThread。

請注意,執行線程充滿了危險。你將不得不確保你的代碼是線程安全的,你可能會得到不可預知的結果。

而且,這裏有其他計算器的答案,可以幫助:

Using NSThreads in Cocoa?

Where can I find a good tutorial on iPhone/Objective-C multithreading?


原來的答覆:

不要執行搜索,直到用戶按下「搜索」按鈕。

有一個委託方法可以實現趕上按下搜索按鈕的:

- (void)searchBarSearchButtonClicked:(UISearchBar *)searchBar; 
+0

不正是我期待的...我已經看到它在用戶鍵入之前完成,有延遲加載表格,但它與可以自由輸入大型數據庫的鍵盤分開。 – Kulpreet 2009-07-29 07:41:00