[英]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:
:,但是还有另一种更方便的方式:
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.