【问题标题】:Why doesn't my UISearchDisplayController use my custom tableViewCell?为什么我的 UISearchDisplayController 不使用我的自定义 tableViewCell?
【发布时间】:2012-07-23 13:23:45
【问题描述】:

我有一个使用拆分视图控制器的 iPad 应用程序。在主视图中,我使用情节提要中定义的自定义表格单元格显示了一个项目列表。

单元格按照我在 Xcode 中选择的深色背景完全显示。

我正在使用UISearchBarUISearchDisplayController

当我开始搜索时,列表变为标准的白色表格单元格。

如何让 SearchDisplayController 使用我的自定义单元格?

当我在搜索字段中键入时,回调会更新过滤结果列表:

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

    return YES;
}

我的表格视图处理使用它来显示完整列表或过滤列表。 CellIdentifier 与情节提要中单元格的标识符相匹配:

- (NSInteger)tableView:(UITableView *)tableView numberOfRowsInSection:(NSInteger)section
{
    if (tableView == self.searchDisplayController.searchResultsTableView)
        return [self.filteredAvailableChannel count];
    else
        return [[self availableChannelList] count];
}

- (UITableViewCell *)tableView:(UITableView *)tableView
         cellForRowAtIndexPath:(NSIndexPath *)indexPath
{
    static NSString *CellIdentifier = @"AvailableChannelCell";
    FVAvailableChannelCell *cell = [self.tableView dequeueReusableCellWithIdentifier:CellIdentifier];

    // Configure the cell...
    FVChannel *channel = nil;
    int row = [indexPath row];
    if (tableView == self.searchDisplayController.searchResultsTableView)
        channel = [self.filteredAvailableChannel objectAtIndex:row];
    else
        channel = [[self availableChannelList] objectAtIndex:row];

    cell.idLabel.text = channel.channelID;
    cell.nameLabel.text = channel.channelName;
    cell.unitLabel.text = channel.unitName;

    return cell;
}

为什么我的 searchDisplayController 不使用我的自定义单元格?

2012 年 7 月 31 日更新

在两次和三次检查所有情节提要的连接(似乎是正确的)之后,我注意到了一些事情……

我可以看到我实际上是获取一个自定义单元格 - 它只是 看起来 像一个标准单元格。

在我修复tableView:heightForRowAtIndexPath: 之后,我注意到了一些东西。当我选择一个单元格时,我可以看到自定义单元格在那里,但由于它似乎忽略了我的自定义单元格的背景颜色,我将自定义白色文本放在标准白色背景之上,使其看起来像空单元格。

自定义单元格类中的initWithStyle:reuseIdentifier: 中的断点永远不会被击中,因此某些地方不正确。

任何指针:

  1. 在 SearchResultsController 中的单元格上获取自定义背景颜色时缺少什么?
  2. 为什么我的自定义单元格的 initWithStyle 没有被命中?它被设置为情节提要中单元格的类。

【问题讨论】:

    标签: ipad uitableview uisearchbar custom-cell uisearchbardisplaycontrol


    【解决方案1】:

    Apple 开发者论坛上的反馈以及以下帖子很有帮助:

    How to customize the background color of a UITableViewCell?

    简单来说,覆盖tableView:willDisplayCell:,就可以适当设置背景色了。

    【讨论】:

    • 哇。 10 天后回答我自己的问题投反对票 - 我仍然认为这是一个正确的答案。有人需要重新服药。
    猜你喜欢
    • 2015-07-03
    • 1970-01-01
    • 1970-01-01
    • 2019-06-30
    • 1970-01-01
    • 2018-05-30
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    相关资源
    最近更新 更多