【问题标题】:Custom Cell not working in UITableView for Search Results自定义单元格在 UITableView 中无法用于搜索结果
【发布时间】:2015-03-31 19:31:00
【问题描述】:

首先我想说的是,我已经研究并尝试关注 UISearchDisplayController and custom cell 等 stackoverflow 链接,但问题仍然存在

我的 UITableView 中集成了搜索栏和搜索显示控制器。搜索功能工作正常,但搜索结果单元格具有默认的白色单元格设计,而不是用于 UITableView 的自定义单元格设计。下面是使搜索结果的单元格设计适应我的自定义设计的代码。

- (void)viewDidLoad
{
    [super viewDidLoad];
    [self.searchDisplayController.searchResultsTableView registerClass:[ItemCellTableViewCell class] forCellReuseIdentifier:@"Cell"];
    if(!self.document){
        [self initDocument];
    }
    self.filteredItemCatalogArray = [[NSMutableArray alloc]init];
    self.itemCatalogTableView.dataSource = self;
    self.itemCatalogTableView.delegate = self;
    [self.itemCatalogTableView reloadData];
    self.itemCatalogTableView.backgroundColor = [UIColor clearColor];
    self.itemCatalogTableView.opaque = NO;
    self.itemCatalogTableView.bounces = YES;
    self.itemCatalogTableView.backgroundView = nil;
}

- (UITableViewCell *)tableView:(UITableView *)tableView cellForRowAtIndexPath:(NSIndexPath *)indexPath
{
    static NSString *CellIdentifier = @"Cell";
    if(tableView == self.searchDisplayController.searchResultsTableView)
    {
        UITableViewCell *cell = [tableView dequeueReusableCellWithIdentifier:CellIdentifier];
        //cell fields customization
        cell.backgroundColor = [UIColor clearColor];
        cell.opaque = NO;
        return cell;
    }
    else
    {
        ItemCellTableViewCell *cell = [tableView dequeueReusableCellWithIdentifier:CellIdentifier forIndexPath:indexPath];
        //cell fields customization
        cell.backgroundColor = [UIColor clearColor];
        cell.opaque = NO;
        return cell;
    }
}

我在这里错过了什么?

已编辑:

在搜索结果的 if 块中,我将 tableview 更改为 self.tableview 并获得正确的自定义单元格。但它采用了不正确的高度,该高度较小,因此与搜索结果的行重叠

【问题讨论】:

  • 在搜索结果的 if 块中,我将 tableview 更改为 self.tableview 并获得了正确的自定义单元格。但它采用了不正确的高度,该高度较小,因此与搜索结果的行重叠
  • 为了纠正高度问题,我在 viewdidload self.searchDisplayController.searchResultsTableView.rowHeight = self.tableView.rowHeight 中添加了以下行;

标签: ios uitableview uisearchbar uisearchdisplaycontroller


【解决方案1】:

在搜索结果的 if 块中,我将 tableview 更改为 self.tableview,它得到了正确的自定义单元格。但它的高度不正确,因此与搜索结果的行重叠

为了纠正高度问题,我在 viewdidload 中添加了以下行

self.searchDisplayController.searchResultsTableView.rowHeight = self.tableView.rowHeight ;

【讨论】:

  • 这只修复了单行。
  • 如果搜索匹配所有条目,这将不起作用。 tableView 中的所有单元格都将出列,高度崩溃。将高度值设置为以像素为单位的实际值。
  • 这是完美的答案!!对我来说很有魅力!!感谢@Aditya parikh
【解决方案2】:

1) 如果您的单元格使用的是 xib 文件,您应该添加到 viewDidLoad(或在 tableView 委托之前调用的其他方法)

[yourTableView registerNib:[UINib nibWithtName:@"your_nibName" bunde:yourBunde] forCellReuseIdentifier:@"yourIdentifier"]

您也应该为自定义单元格使用注册标识符:

- (UITableViewCell *)tableView:(UITableView *)tableView cellForRowAtIndexPath:(NSIndexPath *)indexPath
{
    static NSString *CellIdentifier = @"Cell";
    static NSString *CustomCellIdentifier  = @"yourIdentifier";
    if(tableView == self.searchDisplayController.searchResultsTableView)
    {
        // UITableViewCell customization
      return cell;
    }
    else
    {
        ItemCellTableViewCell *cell = [tableView dequeueReusableCellWithIdentifier:CustomCellIdentifier forIndexPath:indexPath];
          if (!cell)
                cell = [[ItemCellTableViewCell alloc] initWithStyle:yourPreferedStyle reuseIdentifier:CustomCellIdentifier] // or other custom initialization

        //put cell fields customization here
        return cell;
    }
}

【讨论】:

    【解决方案3】:

    如果您的自定义单元格是使用 NIB 设计的,请尝试此操作

    [self.searchDisplayController.searchResultsTableView registerNib:[UINib nibWithNibName:@"YourCellNibName" bundle:Nil] forCellWithReuseIdentifier:@"Cell"];
    

    【讨论】:

    • 我的自定义单元格是使用 XCode 5 中的 Interface Builder 设计的
    • 是的,同理,XIB文件也叫NIB。
    • 哦好吧,在你的 cellForRowAtIndexPath 和 if(tableView == self.searchDisplayController.searchResultsTableView) 你说的是 UITableViewCell *cell = [tableView dequeueReusableCellWithIdentifier:CellIdentifier]; 不应该是 ItemCellTableViewCell *cell = [tableView dequeueReusableCellWithIdentifier:CellIdentifier];
    • 是的,用于自定义我需要 ItemCellTableViewCell 的自定义标签,但如果不为“单元格字段自定义”添加任何行,即使这样,它也应该显示在界面生成器中定义的默认自定义单元格,因为我在其中注册了它viewdidload 方法。
    • 但是 UITableViewCell 类的实例如何引用您的自定义标签? register 类方法只是让dequeueReusableCellWithIdentifier 返回一个非零单元格。您仍然需要获取 ItemCellTableViewCell 的实例才能显示您的自定义标签。
    【解决方案4】:

    你必须得到你的 tableView 的参考,然后在 - (UITableViewCell *)tableView:(UITableView *)tableView cellForRowAtIndexPath:(NSIndexPath *)indexPath { static NSString *CellIdentifier = @"CustomTableCell"; MyCell *cell = (MyCell *)[self.tableView dequeueReusableCellWithIdentifier:CellIdentifier]; ... }

    【讨论】:

      猜你喜欢
      • 1970-01-01
      • 1970-01-01
      • 2016-02-06
      • 2012-05-24
      • 1970-01-01
      • 1970-01-01
      • 2011-11-29
      • 1970-01-01
      • 1970-01-01
      相关资源
      最近更新 更多