【问题标题】:Search bar results not showing in tableview搜索栏结果未显示在表格视图中
【发布时间】:2014-02-06 00:12:59
【问题描述】:

我创建了一个搜索 tableview 内容的 Search 方法。这很好用,每次我在搜索栏中写入时,它都会将对象添加到一个名为 filtersArray 的数组中,并显示等于 filtersArray.count 的 tableviewcells 的数量。我已经用 NSLog(@"%@", 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];
}

【问题讨论】:

  • 显示您的搜索委托方法代码。
  • 完成。我添加了方法和 2 张图片来说明我什么时候不搜索和什么时候搜索。
  • LanguageCell - 单元格是否已在表格中注册? cell = [[LanguageCell alloc] initWithStyle:... 何时运行?
  • 它注册在一个名为 UITableViewCell 的类中。 languageCell.h 和 .m
  • 这不是注册的意思(即定义)。调试并告诉我cell = [[LanguageCell alloc] initWithStyle:... 何时运行。

标签: ios objective-c uitableview uisearchbar


【解决方案1】:

你可以在添加一个新字符后使 [yourTableView reloadData]

【讨论】:

    【解决方案2】:

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

    当搜索表视图没有得到一个出队的单元格时,您使用initWithStyle:reuseIdentifier: 创建一个单元格,但这不会创建您的patternLabel。因此,您不会发生崩溃,因为您返回了一个单元格,但该单元格是空的,因为标签不存在。

    您的最佳选择:

    1. 删除原型单元格,创建一个 XIB 并在每个表视图中显式注册 NIB
    2. 不使用搜索控制器(因此您只有 1 个表格视图,您自己管理搜索栏)

    【讨论】:

      猜你喜欢
      • 2021-09-24
      • 2017-02-28
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      • 2016-10-28
      • 2021-01-25
      • 1970-01-01
      相关资源
      最近更新 更多