2016-03-01 44 views
2

自定義單元格,我想實現一個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 
} 


} 
+0

您的resultsTableController是否存在於代碼或故事板中?如果前者,你的單元格是在故事板中原型的?如果後者,你的tableView調用'registerNib:'或'registerClass:forCellReuseIdentifier'? – Quantaliinuxite

+0

resultController同時存在於代碼和故事板中,我將它與故事板中的TableViewController以及我的主要VC關聯到searchController'self。searchController = UISearchController(searchResultsController:resultsTableController);'和Cell是原型的,並且與我的自定義單元類相關聯(也與IBoutlet鏈接) – Akhu

+0

是的,但是你像這樣調用init'resultsTableController = ProductResultTabController();'你能發佈'ProductResultTabController'代碼 – Quantaliinuxite

回答

4

據我瞭解,你ProductResultTabController生活與電池原型故事板。因此,調用resultsTableController = ProductResultTabController()不會創建TableViewController的最重要部分,它是註冊您的tableView:用於類或筆尖。你可以在筆尖創建細胞,並在您PRTC viewDidLoad:打電話tableview.registerNib:,但有一個其他的,稍微更方便的方法:

  1. 在故事板創建PRTC和原型細胞(我相信你已經做到這一點)
  2. 在SB,去屬性檢查器,然後給控制器故事板ID

Giving a storyboard ID in Attributes Inspector

3 - 在你的主VC的viewDidLoad:,更換resultsTab leController = ProductResultTabController()`:

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

非常感謝!它完美的作品:D – Akhu

+0

沒問題!這是我的榮幸 :) – Quantaliinuxite

相關問題