2009-07-29 23 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