目前我填充我的tableviewcells與多個陣列的代表名稱,ID等SearchDisplayController搜索多個陣列
我的問題涉及的內容,當我開始使用搜索顯示控制器。我有一個包含名稱列表,ID列表,條形碼列表和別名列表的數組。當用戶鍵入搜索欄時,我需要能夠搜索所有4個數組。當它發現在1個陣列它具有配對的結果與3個其他陣列的結果..
例
- 名稱(蘋果,胡蘿蔔,香蕉,狗)
- 別名(紅,橙,黃色,棕色)
- 條形碼(1,2,10,20)
- ID(30,40,50,60)
因此,如果用戶鍵入 「一個」 我應該填充表查看 蘋果,胡蘿蔔,香蕉和相關的別名,條碼,身份證。
如果用戶輸入2我只能得到 胡蘿蔔和狗。
如果用戶輸入0,我會得到所有這些項目。
任何想法如何做到這一點?
更新: 這就是我做到的。
-(BOOL)searchDisplayController:(UISearchDisplayController *)controller shouldReloadTableForSearchString:(NSString *)searchString {
BOOL shouldReturn = FALSE;
[searchResults removeAllObjects];
for (int i = 0; i < [itemIDRows count]; i++) {
BOOL foundResult = FALSE;
if ([[itemIDRows objectAtIndex:i] rangeOfString:searchString].location != NSNotFound) {
foundResult = TRUE;
}
if ([[nameRows objectAtIndex:i] rangeOfString:searchString].location != NSNotFound) {
foundResult = TRUE;
}
if ([[barcodeRows objectAtIndex:i] rangeOfString:searchString].location != NSNotFound) {
foundResult = TRUE;
}
if ([[aliasRows objectAtIndex:i] rangeOfString:searchString].location != NSNotFound) {
foundResult = TRUE;
}
if (foundResult) {
NSNumber *result = [NSNumber numberWithInt:i];
if ([self searchResults] == nil) {
NSMutableArray *array = [[NSMutableArray alloc] init];
[self setSearchResults:array];
[array release];
}
[searchResults addObject:result];
shouldReturn = YES;
}
}
return shouldReturn;
}
然後當我填充的tableview我做這樣的事
if ([tableView isEqual:self.searchDisplayController.searchResultsTableView]) {
[cell setCellContentsName:[NSString stringWithFormat:@"%@", [nameRows objectAtIndex:[[searchResults objectAtIndex:indexPath.row] integerValue]]];
} else {
[cell setCellContentsName:[NSString stringWithFormat:@"%@", [nameRows objectAtIndex:indexPath.row]];
}
然而,當我鍵入類似9999它帶來了情況,其中只有1個9是在ID或條形碼。任何想法如何解決這個問題?
UPDATE2: 通過讓列表總是刷新來解決問題,而不是僅在找到結果時重新加載數據。現在,它完美的作品:d