UISearchBar没有常见的tableViewCell

时间:2015-05-27 22:49:12

标签: ios objective-c xcode uitableview uisearchbar

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

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

如果我在mainTableView和searchResultsTableView的cellForRowAtIndexPath中使用Storyboard ID,则会发生崩溃:'无法使用标识符SearchResultCell对单元格进行出列 - 必须为该单元格注册nib或类标识符或连接故事板中的原型单元格。

非常感谢任何帮助!

1 个答案:

答案 0 :(得分:1)

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

enter image description here

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

enter image description here

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

enter image description here

然后声明该类的属性:

@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未注册。希望我能帮助你......快乐的编码欢呼......