Akh*_*khu 2 uitableview custom-cell ios swift uisearchcontroller
我正在尝试实现一个 UISearchController(而不是已弃用的 UISearchDisplayController)
我面临着一个可笑的时间进食问题。
当我尝试时,dequeueResusableCellWithIdentifier它不适用于我的CellAutocompletion(UITableViewCell 子类化)
像这样 :
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
}
Run Code Online (Sandbox Code Playgroud)
这个扔我 fatal error: unexpectedly found nil while unwrapping an Optional value
但是当我这样做时:
let cell = UITableViewCell(style: UITableViewCellStyle.Default, reuseIdentifier: "suggestionCell");
Run Code Online (Sandbox Code Playgroud)
有用。因此,对于默认的 iOS 单元,它可以工作,而我的自定义单元则不能。任何的想法?
我想我问了错误的tableview,但考虑到我在另一个VC使用的UISearchController中包含的TableviewController中,我迷路了。
我的主要 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
Run Code Online (Sandbox Code Playgroud)
我的 CustomCell 类
class CellAutocompletion:UITableViewCell{
@IBOutlet weak var itemLabel: UILabel!
@IBOutlet weak var parentItemLabel: UILabel!
}
Run Code Online (Sandbox Code Playgroud)
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
}
}
Run Code Online (Sandbox Code Playgroud)
据我所知,你的ProductResultTabController生活在故事板中的单元原型。因此, callresultsTableController = ProductResultTabController()并没有完成创建 a 的最重要部分TableViewController,即注册您的 tableView: 用于类或笔尖。您可以在 nib 中创建您的单元格并调用tableview.registerNib:您的 PRTC viewDidLoad:,但还有另一种稍微方便的方法:
3 - 在您的主 VC 中viewDidLoad:,将 resultsTableController = ProductResultTabController()`替换为以下内容:
let storyboard = UIStoryboard(name: "Main", bundle: nil)
resultsTableController = storyboard.instantiateViewControllerWithIdentifier(myStoryboardID)
Run Code Online (Sandbox Code Playgroud)