搜索栏结果未显示在tableview中

时间:2014-02-05 15:30:26

标签: ios objective-c uitableview uisearchbar

我创建了一个Search方法,用于搜索tableview的内容。这很好用,每次我在搜索栏中写入时,它都会将对象添加到名为filteredArray的数组中,并显示等于filteredArray.count的tableviewcells的数量。我用NSLog(@“%@”,filteredArray)测试了这个;

问题是我想在你搜索时在tableview中显示filteredArray。我为此改变了cellForRowAtIndexPath方法,但它只给了我空的tableviewcells。我做错了什么?

不搜索:

enter image description here

搜索“ru”:

enter image description here

- (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];
}

2 个答案:

答案 0 :(得分:1)

添加新角色后可以制作[yourTableView reloadData]

答案 1 :(得分:0)

你有2个表格视图,单元格注册了一个而不是另一个 - 这是原型单元格在故事板中定义时的工作方式,我想你已经完成了。

如果搜索表视图未显示已出列的单元格,则您使用initWithStyle:reuseIdentifier:创建一个但不会创建patternLabel。所以,你没有崩溃,因为你返回一个单元格,但是单元格是空的,因为标签不存在。

您最好的选择:

  1. 删除原型单元格,创建XIB并明确向每个表格视图注册NIB
  2. 不使用搜索控制器(因此您只有1个表视图,自己管理搜索栏)
相关问题