【问题标题】:UISearchBar without a common tableViewCell没有通用 tableViewCell 的 UISearchBar
【发布时间】:2015-08-10 05:52:35
【问题描述】:

我正在按照这个示例在我的项目中设置 UISearchController: https://developer.apple.com/library/ios/samplecode/TableSearch_UISearchController/Introduction/Intro.html#//apple_ref/doc/uid/TP40014683-Intro-DontLinkElementID_2

在示例中,他们以编程方式设置了一个 tableViewCell 用于主表视图和搜索结果,而我已经在主表视图和搜索结果的表视图中获得了我将使用的单元格。

如果我在 mainTableView 和 searchResultsTableView 的 cellForRowAtIndexPath 中使用 Storyboard ID,则会发生崩溃:“无法将具有标识符 SearchResultCell 的单元格出列 - 必须为标识符注册一个 nib 或一个类,或者连接故事板中的原型单元格”。

非常感谢任何帮助!

【问题讨论】:

  • 最简单的方法是使用 .xib..

标签: ios objective-c xcode uitableview uisearchbar


【解决方案1】:

我的意思是像这样创建一个 .xib:

配置单元格的UI 并设置标识符,例如:tableID

为属性(IBOutlet)设置自定义类dTableViewCell

然后为类声明一个属性:

@property (nonatomic) dTableViewCell *tableCell;
@property (nonatomic) dSearchTableViewCell *tableCell;

然后在你的UITableViewDelegate 里面:

- (UITableViewCell *)tableView:tableView cellForRowAtIndexPath:indexPath
{

    if (tableView == self.mainTableView)
    {
        static NSString *cellID = @"tableID";

        //dTableViewCell.xib for mainTableView ]> example...
        [tableView registerNib:[UINib nibWithNibName:@"dTableViewCell" bundle:nil] forCellReuseIdentifier:cellID];

        self.tableCell = [tableView dequeueReusableCellWithIdentifier:cellID];
    }
    else //searchTableView
    {
        static NSString *cellSearchID = @"searchTableID";

        //dSearchTableViewCell.xib for searchTable ]> example...
        [tableView registerNib:[UINib nibWithNibName:@"dSearchTableViewCell" bundle:nil] forCellReuseIdentifier:cellSearchID];

        self.searchTableCell = [tableView dequeueReusableCellWithIdentifier:cellID];
    }

}

还要检查你的故事板中的 SearchResultCell 标识符,因为崩溃告诉你 SearchResultCell 没有注册。希望我能帮助你。祝你编码愉快。

【讨论】:

  • 非常感谢。我试过了,但不幸的是它不适用于我设置它的方式。它与 Apple 示例中的一样,但带有情节提要。所以我在主表视图和搜索表视图类中都设置了 cellForRowAtIndexPath。如果我在我的搜索类中使用我为搜索创建的单元格,它就像以前一样崩溃。我可以使其工作如下:github.com/dempseyatgithub/Sample-UISearchController 但问题是进入详细视图时搜索栏仍然存在......
猜你喜欢
  • 1970-01-01
  • 1970-01-01
  • 1970-01-01
  • 2014-05-26
  • 2017-09-17
  • 1970-01-01
  • 1970-01-01
  • 1970-01-01
  • 1970-01-01
相关资源
最近更新 更多