[英]Search bar results not showing in tableview
我創建了一個Search方法,用於搜索tableview的內容。 這工作正常,並且每次我在搜索欄中書寫時,它都會將該對象添加到名為filteredArray的數組中,並顯示等於filteredArray.count的tableviewcell的數量。 我已經用NSLog(@“%@”,filteredArray)進行了測試;
問題是我想在搜索時在表視圖中顯示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];
}
您可以在添加新字符后制作[yourTableView reloadData]
您有2個表格視圖,該單元格是向一個單元注冊的,但沒有另一個-這是您在情節提要中定義原型單元格的工作方式,我想您已經完成了。
當搜索表視圖沒有出列單元格時,可以使用initWithStyle:reuseIdentifier:
創建一個單元格,但這不會創建patternLabel
。 因此,您不會崩潰,因為您返回了一個單元格,但是該單元格為空,因為標簽不存在。
您的最佳選擇:
聲明:本站的技術帖子網頁,遵循CC BY-SA 4.0協議,如果您需要轉載,請注明本站網址或者原文地址。任何問題請咨詢:yoyou2525@163.com.