2014-09-27 109 views
0

在我的應用程序中,我使用帶有可搜索的集合視圖來讓用戶找到存儲在coredata中的人員記錄。我有搜索設置在每個字符輸入時運行。起初,這個工作很好,但現在我有大量的數據,用戶界面是滯後的。我想我需要使用另一個線程來做到這一點,但我是多線程的全神貫注。iOS更快地搜索集合視圖

這裏是我的代碼:

-(void)searchBar:(UISearchBar *)searchBar textDidChange:(NSString *)searchText { 
    self.fetchedResultsController = nil; 

    NSError *error = nil; 
    if (![[self fetchedResultsController]performFetch:&error]) { 
     abort(); 
    } 

    [self.collectionView reloadData]; 
    [self.searchBar becomeFirstResponder]; 
} 

有沒有辦法做到這速度嗎?

編輯:我應該指出的是,搜索字符串被送入他在獲取謂語...

回答

1

我已使用THIS從數據庫的UITABLEVIEW的方法,它的工作就像魅力。 你是如何實現的:

- (UICollectionViewCell *)collectionView:(UICollectionView *)collectionView cellForItemAtIndexPath:(NSIndexPath *)indexPath 

您能不能告訴我們高度的代碼以及,

一些代碼示例:

- (void)searchBar:(UISearchBar *)searchBar textDidChange:(NSString *)searchText 
{ 
self.feed.counter = 50; 

totalcustomers = [cust getTotalCustomer:@"textSearch" searchText:searchtextString custType:YES employeeId:@"" custId:[module getCurrentOrderCustomerId]]; 

     self.currentSearchTitle = searchText; 

     [cust setDatabasePath:DELEGATE.databasePath]; 

     self.feed.largeArray = (NSMutableArray *) [cust getSeachedCustomer:searchtextString custId:[module getCustomerId:[NSNumber numberWithInt:0]] action:@"textSearch"]; 

     self.availableCustomers = self.feed.largeArray; 

     [self.allCustomerTable reloadData]; 

     NSLog(@"%d ",totalcustomers); 
} 

和UITableviewScroll加載接下來的50條記錄:

- (void)tableViewOverridedForScrollWithSearch:(UITableView *)tableView 
        didSelectRowAtIndexPath:(NSIndexPath *)indexPath 
{ 
NSArray * newPosts; 

      newPosts = [self.feed newRowsFrom:indexPath.row andAction:@"textSearch" andSearchString:self.currentSearchTitle]; 
      NSUInteger newCount = [newPosts count]; 



      if (newCount) { 

       [self.availableCustomers addObjectsFromArray:newPosts]; 

       [self.allCustomerTable performSelectorOnMainThread:@selector(reloadData) withObject:nil waitUntilDone:NO]; 
       return; 
      } 
     } 

} 

and get next 50

- (NSArray *)newRowsFrom:(NSUInteger)newItem andAction:(NSString *)action andSearchString:(NSString *)searchString{ 

NSLog(@"Counter 1 Print >>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>> %d", self.counter); 

NSArray *result = [self newRowsFromDatabase:self.counter andAction:action andSearchString:searchString]; 

self.counter = self.counter + 50; 
NSLog(@"Counter 2 Print >>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>> %d", self.counter); 
return result; 

}

希望它會幫助你 感謝

+0

我沒有得到這將如何加快從UISearchBar搜索... 我的cellForItemAtIndexPath是非常標準的。創建一個單元格,從fetchedresults控制器獲取實體對象,設置單元格屬性...我沒有代碼高度... – BBruce 2014-09-27 16:43:01

+0

如果您看到有兩件事涉及:1.搜索和2.loareload數據集合。因此,如果數據較少,則工作正常。因此,如果用戶輸入「a」,則只需獲得前50個數據,更新數據源並在用戶滾動時重新加載collectionview.now,然後加載下一個50數據等等,我已經在我的應用程序中使用通用引擎從搜索,索引,滾動到下面實現了這樣的功能,該引擎從數據中獲取(搜索數據/簡單的沒有搜索數據,索引數據)base.i將在一段時間內顯示代碼片段 – Alok 2014-09-27 17:19:34

1

一次提取所有結果,並將它們保存在一個數組。然後在該數組中搜索,而不是每次調用該方法時從核心數據中獲取數據。

+0

感謝。我從來沒有將一個集合視圖或表視圖連接到一個數組。我會研究它。 – BBruce 2014-09-27 16:38:41