自定義單元格,我想實現一個UISearchController(不UISearchDisplayController這是不建議使用)與UISearchController中的UITableViewController(結果控制器)
我面對一個可笑的時間吃的問題。
當我嘗試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
}
這把我fatal error: unexpectedly found nil while unwrapping an Optional value
但當我這樣做:
let cell = UITableViewCell(style: UITableViewCellStyle.Default, reuseIdentifier: "suggestionCell");
它的工作原理。所以對於默認的iOS單元,它可以工作,而我的自定義單元不適用。任何想法?
我想我問了錯誤的tableview,但考慮到我在一個包含在UISearchController裏面的TableviewController被另一個VC使用,我迷路了。
我主要的VC它instanciate我的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
我CustomCell類
class CellAutocompletion:UITableViewCell{
@IBOutlet weak var itemLabel: UILabel!
@IBOutlet weak var parentItemLabel: UILabel!
}
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
}
}
您的resultsTableController是否存在於代碼或故事板中?如果前者,你的單元格是在故事板中原型的?如果後者,你的tableView調用'registerNib:'或'registerClass:forCellReuseIdentifier'? – Quantaliinuxite
resultController同時存在於代碼和故事板中,我將它與故事板中的TableViewController以及我的主要VC關聯到searchController'self。searchController = UISearchController(searchResultsController:resultsTableController);'和Cell是原型的,並且與我的自定義單元類相關聯(也與IBoutlet鏈接) – Akhu
是的,但是你像這樣調用init'resultsTableController = ProductResultTabController();'你能發佈'ProductResultTabController'代碼 – Quantaliinuxite