【问题标题】:Cells in UITableView keep clear when searching in UISearchBar在 UISearchBar 中搜索时,UITableView 中的单元格保持清晰
【发布时间】:2014-03-14 04:22:32
【问题描述】:

我有一个包含两个部分的UITableView 应用程序。现在我想添加一个UISearchBar 来允许搜索。并且搜索确实工作得很好并选择了正确的单元格。搜索时,您可以看到可以选择的单元格,但标签是空白的。这是我的代码:

- (UITableViewCell *)tableView:(UITableView *)tableView cellForRowAtIndexPath:(NSIndexPath *)indexPath
{
    static NSString *CellIdentifier = @"qrCodeCell";
    QRTableViewCell *cell = (QRTableViewCell*)[tableView dequeueReusableCellWithIdentifier:CellIdentifier];

    if (cell == nil) {
        cell = [[QRTableViewCell alloc] initWithStyle:UITableViewCellStyleDefault reuseIdentifier:CellIdentifier];
    }

    if (indexPath.section == 0) {
        NSDateFormatter *format = [[NSDateFormatter alloc] init];
        [format setDateFormat:@"MMM dd, yyyy HH:mm"];
        if (tableView == self.searchDisplayController.searchResultsTableView) {
            //NSLog(@"%@",[[self.searchResultsQR objectAtIndex:indexPath.row] valueForKey:@"data"]);
            [cell.dataLabel setText:[[self.searchResultsQR objectAtIndex:indexPath.row] valueForKey:@"data"]];
            //NSLog(@"%@",cell.dataLabel.text);

            NSDate *timeStamp = [[self.searchResultsQR objectAtIndex:indexPath.row] valueForKey:@"timeStamp"];
            [cell.timeLabel setText:[format stringFromDate:timeStamp]];
        } else {
            [cell.dataLabel setText:[[self.qrcodes objectAtIndex:indexPath.row] valueForKey:@"data"]];
            NSDate *timeStamp = [[self.qrcodes objectAtIndex:indexPath.row] valueForKey:@"timeStamp"];
            [cell.timeLabel setText:[format stringFromDate:timeStamp]];
        }
    } else if (indexPath.section == 1) {
        NSDateFormatter *format = [[NSDateFormatter alloc] init];
        [format setDateFormat:@"MMM dd, yyyy HH:mm"];
        if (tableView == self.searchDisplayController.searchResultsTableView) {
            [cell.dataLabel setText:[[self.searchResultsScanned objectAtIndex:indexPath.row] valueForKey:@"data"]];
            NSDate *timeStamp = [[self.searchResultsScanned objectAtIndex:indexPath.row] valueForKey:@"timeStamp"];
            [cell.timeLabel setText:[format stringFromDate:timeStamp]];
        } else {
            [cell.dataLabel setText:[[self.scannedCodes objectAtIndex:indexPath.row] valueForKey:@"data"]];
            NSDate *timeStamp = [[self.scannedCodes objectAtIndex:indexPath.row] valueForKey:@"timeStamp"];
            [cell.timeLabel setText:[format stringFromDate:timeStamp]];
        }
    }
    return cell;
}

这与这些问题中的问题几乎完全相同,但问题无法解决:UISearchBar with UITableView containing custom cells => blank cellsIt doesn't show the contents of the cells in a UITableView filtered by a UISearchDisplayController

如果有人可以帮助我,我将不胜感激!

编辑:

这里有更多来自我的TableViewController的代码:

- (void)filterContentForSearchText:(NSString*)searchText scope:(NSString*)scope
{
    NSPredicate *resultPredicate = [NSPredicate predicateWithFormat:@"data contains[c] %@", searchText];
    self.searchResultsQR = [self.qrcodes filteredArrayUsingPredicate:resultPredicate];
    self.searchResultsScanned = [self.scannedCodes filteredArrayUsingPredicate:resultPredicate];
}

-(BOOL)searchDisplayController:(UISearchDisplayController *)controller shouldReloadTableForSearchString:(NSString *)searchString
{
    [self filterContentForSearchText:searchString
                               scope:[[self.searchDisplayController.searchBar scopeButtonTitles]
                                      objectAtIndex:[self.searchDisplayController.searchBar
                                                     selectedScopeButtonIndex]]];

    return YES;
}

【问题讨论】:

  • 搜索时是否返回了正确的行数,并在搜索时确认数组中的值是什么?
  • 是的,这部分工作得很好。正如您在我的代码中看到的那样,我通过NSLog 检查了它,然后它是否已写入标签,但它没有。
  • 能否也分享一下searchDisplayController Delegate中写的代码
  • 最后一件事是你使用storyboards还是xib?
  • 我正在使用故事板。我的 tableViewController 是委托,所以我会将方法添加到我的帖子中。

标签: ios objective-c uitableview uisearchbar uisearchdisplaycontroller


【解决方案1】:

您的手机未正确注册。这意味着您的代码将运行:

cell = [[QRTableViewCell alloc] initWithStyle:UITableViewCellStyleDefault reuseIdentifier:CellIdentifier];

这不会创建您的所有子视图或连接任何出口。

您需要找出您的手机未正确注册的原因。如果您在情节提要中创建它,那么它应该包含在将使用它的表视图中(应该自动注册单元格)。如果您在独立的 XIB 文件中创建它,则需要加载 NIB 并在加载表视图时显式注册它。

您有 2 个表格视图,单元格注册到其中一个而不是另一个 - 这是原型单元格的工作方式。

您的最佳选择:

-删除原型单元格,创建一个 XIB 并在每个表视图中显式注册 NIB

-不使用搜索控制器(所以你只有1个表格视图,自己管理搜索栏)

These are a few references which you can look forward to.

This is a tutorial for implementing search delgates with storyboard

还发现了一个关于 SO 的讨论,它可能会给你一些关于你哪里出错的方向

Search Bar in UITableView doesn't display text in Cell label

【讨论】:

  • 在仔细调试所有内容的同时,我提到了很多事情都不是它应该的样子(例如,单元格突然变成了 nil)。但是,当您将您的建议外包到单独的 XIB 文件中时,现在一切都像魅力一样!非常感谢!!
猜你喜欢
  • 1970-01-01
  • 1970-01-01
  • 2016-10-26
  • 1970-01-01
  • 2014-06-20
  • 1970-01-01
  • 1970-01-01
  • 1970-01-01
  • 2022-01-10
相关资源
最近更新 更多