我已經創建了一個搜索方法,它可以搜索tableview的內容。這工作正常,每次我在搜索欄中寫入時,它都會將該對象添加到名爲filteredArray的數組中,並顯示等於filteredArray.count的tableviewcell的數量。我用NSLog測試了這個(@「%@」,filteredArray);搜索欄結果不會顯示在桌面視圖中
問題是我想在你搜索時在tableview中顯示filteredArray。我已經改變了這個cellForRowAtIndexPath方法,但它只是給我空的tableviewcells。我究竟做錯了什麼?
不搜索:
搜索 「RU」:
- (UITableViewCell *)tableView:(UITableView *)tableView cellForRowAtIndexPath:(NSIndexPath *)indexPath
{
static NSString *CellIdentifier = @"Cell";
LanguageCell *cell = [tableView dequeueReusableCellWithIdentifier:CellIdentifier];
if (!cell) {
cell = [[LanguageCell alloc] initWithStyle:UITableViewCellStyleDefault reuseIdentifier:CellIdentifier];
cell.accessoryType = UITableViewCellAccessoryDisclosureIndicator;
}
if (tableView == self.tableViewData) {
cell.patternLabel.text = [NSString stringWithFormat:@"%@", [[rows objectAtIndex:indexPath.row]objectAtIndex:0]];
} else{
cell.patternLabel.text = [NSString stringWithFormat:@"%@", [filteredArray objectAtIndex:indexPath.row]];
}
return cell;
}
搜索方法:
-(void) searchThroughdata {
self.filteredArray = nil;
NSPredicate *predicate = [NSPredicate predicateWithFormat:@"SELF contains [c] %@",self.searchBar.text];
self.filteredArray = [[finalArray filteredArrayUsingPredicate:predicate] mutableCopy];
NSLog(@"%@", filteredArray);
}
-(void)searchBar:(UISearchBar *)searchBar textDidChange:(NSString *)searchText {
[self searchThroughdata];
}
顯示您的搜索委託方法代碼。 – Wain
完成。我已經添加了方法和2張圖片,說明了當我不搜索時以及當我搜索時。 – user3258468
'LanguageCell' - 是否在表格中註冊了單元格?什麼時候'cell = [[LanguageCell alloc] initWithStyle:...'正在運行? – Wain