简体   繁体   English

UITableViewController(结果控制器)中带有UISearchController的自定义单元格

[英]Custom cell with UISearchController in UITableViewController (result controller)

I am trying to implement a UISearchController (not UISearchDisplayController which is deprecated) 我正在尝试实现UISearchController(而不是已弃用的UISearchDisplayController)

I face a ridiculously time eating issue. 我面临着一个荒唐的吃饭时间问题。

When I try to dequeueResusableCellWithIdentifier it doesn't works with my CellAutocompletion (UITableViewCell subclassing) 当我尝试dequeueResusableCellWithIdentifier它不与我的作品CellAutocompletion (UITableViewCell的子类)

Like this : 像这样 :

override func tableView(tableView: UITableView, cellForRowAtIndexPath indexPath: NSIndexPath) -> UITableViewCell {

    let item = filteredProducts[indexPath.row]

    let cell:CellAutocompletion = self.tableView.dequeueReusableCellWithIdentifier("suggestionCell") as! CellAutocompletion

    cell.itemLabel?.text = item.item_name;

    return cell
}

This throw me fatal error: unexpectedly found nil while unwrapping an Optional value 这会引发fatal error: unexpectedly found nil while unwrapping an Optional value

BUT when I do this : 但是当我这样做时:

    let cell = UITableViewCell(style: UITableViewCellStyle.Default, reuseIdentifier: "suggestionCell");

It works. 有用。 So with a Default iOS cell, it works, with my custom cell not. 因此,对于默认的iOS单元,它可以工作,而对于我的自定义单元,则不能。 Any idea? 任何想法?

I think I ask the wrong tableview but considering I am inside a TableviewController included inside UISearchController which is used by another VC, I am lost. 我想我问错了tableview,但是考虑到我在另一个SearchingVC使用的UISearchController中包含的TableviewController中,我迷路了。

My main VC which instanciate my searchController and the TableViewController where I have issue. 我的主要VC实例化了我的searchController和出现问题的TableViewController。

    //ViewDidLoad
    resultsTableController = ProductResultTabController();
    resultsTableController.tableView.delegate = self;

    self.searchController = UISearchController(searchResultsController: resultsTableController);
    searchController.searchResultsUpdater = self;
    searchController.dimsBackgroundDuringPresentation = true;

    searchController.searchBar.sizeToFit()
    tableView.tableHeaderView = searchController.searchBar

My CustomCell Class 我的CustomCell类别

class CellAutocompletion:UITableViewCell{

@IBOutlet weak var itemLabel: UILabel!
@IBOutlet weak var parentItemLabel: UILabel!

}

ProductResultTabController (subclass of TableViewController) ProductResultTabController (TableViewController的子类)

class ProductResultTabController: UITableViewController {
var filteredProducts = [ITEM]();

override func numberOfSectionsInTableView(tableView: UITableView) -> Int {
    return 1;
}

override func tableView(tableView: UITableView, numberOfRowsInSection section: Int) -> Int {
    return filteredProducts.count
}

override func tableView(tableView: UITableView, cellForRowAtIndexPath indexPath: NSIndexPath) -> UITableViewCell {
    let item = filteredProducts[indexPath.row]

    let cell:CellAutocompletion = self.tableView.dequeueReusableCellWithIdentifier("suggestionCell") as! CellAutocompletion!

    cell.itemLabel?.text = item.item_name;

    return cell
}


}

As I understand, your ProductResultTabController lives in the storyboard with the cell prototype. 据我了解,您的ProductResultTabController与单元原型一起位于情节ProductResultTabController中。 As a consequence, call resultsTableController = ProductResultTabController() doesn't do the most essential part of creating a TableViewController , which is registering your tableView: for a class or nib. 结果,调用resultsTableController = ProductResultTabController()并没有创建创建TableViewController的最重要部分,后者正在为类或笔尖注册tableView:。 You could create your cell in a nib and call tableview.registerNib: in your PRTC viewDidLoad: , but there is an other, slightly more convenient way: 您可以在笔尖中创建单元格,然后在PRTC viewDidLoad:调用tableview.registerNib: :,但是还有另一种更方便的方式:

  1. Create your PRTC in the storyboard and prototype the cell (I believe you've already done that) 在情节提要中创建PRTC并制作单元原型(我相信您已经做到了)
  2. In SB, go to attributes inspector and give the controller a storyboard ID 在SB中,转到“属性检查器”并为控制器提供一个情节提要ID

在属性检查器中提供情节提要ID

3 - In your main VC's viewDidLoad: , replace resultsTableController = ProductResultTabController()`by the following: 3-在主VC的viewDidLoad: ,将以下内容替换为resultsTableController = ProductResultTabController()`:

let storyboard = UIStoryboard(name: "Main", bundle: nil)
resultsTableController = storyboard.instantiateViewControllerWithIdentifier(myStoryboardID)

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

相关问题 UISearchController的自定义单元格 - Custom Cell for UISearchController 用于UISearchController的自定义UITableViewController覆盖了iOS 11中的搜索栏 - Custom UITableViewController for UISearchController overlays the search bar in iOS 11 UISearchController搜索结果单元格背景色 - UISearchController search result cell background color 自定义UITableViewController单元格序列不推送 - custom UITableViewController cell segue not pushing UISearchController不在UITableViewController中搜索 - UISearchController not searching in UITableViewController 带有自定义单元格的ios / swift / UISearchController或SearchDisplayController - Ios/swift / UISearchController or SearchDisplayController with custom cell 如何在UISearchController中使用自定义视图控制器获取结果? - How to use custom view controller in UISearchController for results? UITableViewController页脚内的自定义UITableView单元格 - Custom UITableView Cell inside a UITableViewController footer UILabel不在UITableViewController的自定义原型单元中更新 - UILabels not updating in custom prototype cell of UITableViewController 填充UITableViewController时,自定义单元格中的标签为null - Label in custom cell is null when populating UITableViewController
 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM