[英]Swift Search Bar and Search Display Controller - dequeuereusablecellwithidentifier throws exception
我已經在Xcode 6 beta 6中創建了一個標准的iOS項目,並在Main.Storyboard中添加了搜索欄和搜索顯示控制器。 我還為searchDisplayController添加了幾個委托函數,這些委托函數似乎可以正常工作,但是在搜索時會彈出以下錯誤:
由於未捕獲的異常“ NSInternalInconsistencyException”而終止應用程序,原因:“無法使標識符為Cell的單元出隊-必須為該標識符注冊一個筆尖或一個類,或在情節提要中連接原型單元”
這是我的代碼!
import UIKit
class MasterViewController: UITableViewController, UISearchBarDelegate, UISearchDisplayDelegate {
var detailViewController: DetailViewController? = nil
var objects = [String]()
var filteredObjects = [String]()
override func awakeFromNib() {
super.awakeFromNib()
if UIDevice.currentDevice().userInterfaceIdiom == .Pad {
self.clearsSelectionOnViewWillAppear = false
self.preferredContentSize = CGSize(width: 320.0, height: 600.0)
}
}
override func viewDidLoad() {
super.viewDidLoad()
// Do any additional setup after loading the view, typically from a nib.
self.navigationItem.leftBarButtonItem = self.editButtonItem()
let addButton = UIBarButtonItem(barButtonSystemItem: .Add, target: self, action: "insertNewObject:")
//self.navigationItem.rightBarButtonItem = addButton
if let split = self.splitViewController {
let controllers = split.viewControllers
self.detailViewController = controllers[controllers.count-1].topViewController as? DetailViewController
}
objects = ["Person 1", "Person 2", "Person 3", "Person 4"]
}
override func didReceiveMemoryWarning() {
super.didReceiveMemoryWarning()
// Dispose of any resources that can be recreated.
}
func insertNewObject(sender: AnyObject) {
objects.append("Chris Eatough - 23839")
let indexPath = NSIndexPath(forRow: 0, inSection: 0)
self.tableView.insertRowsAtIndexPaths([indexPath], withRowAnimation: .Automatic)
}
// MARK: - Segues
override func prepareForSegue(segue: UIStoryboardSegue, sender: AnyObject?) {
if segue.identifier == "showDetail" {
let indexPath = self.tableView.indexPathForSelectedRow()
let object = objects[indexPath.row] as String
let controller = (segue.destinationViewController as UINavigationController).topViewController as DetailViewController
controller.detailItem = object
controller.navigationItem.leftBarButtonItem = self.splitViewController.displayModeButtonItem()
controller.navigationItem.leftItemsSupplementBackButton = true
}
}
// MARK: - Table View
override func numberOfSectionsInTableView(tableView: UITableView) -> Int {
return 1
}
override func tableView(tableView: UITableView, numberOfRowsInSection section: Int) -> Int {
if tableView == searchDisplayController.searchResultsTableView {
return filteredObjects.count
}
else {
return objects.count
}
}
override func tableView(tableView: UITableView, cellForRowAtIndexPath indexPath: NSIndexPath) -> UITableViewCell {
// this is where the error happens
let cell = tableView.dequeueReusableCellWithIdentifier("Cell", forIndexPath: indexPath) as UITableViewCell
println("cell has been set to \(cell)")
var object: String
if tableView == searchDisplayController.searchResultsTableView {
object = filteredObjects[indexPath.row] as String
}
else {
object = objects[indexPath.row] as String
}
cell.textLabel.text = object
cell.accessoryType = UITableViewCellAccessoryType.DisclosureIndicator
return cell
}
override func tableView(tableView: UITableView, canEditRowAtIndexPath indexPath: NSIndexPath) -> Bool {
// Return false if you do not want the specified item to be editable.
return true
}
override func tableView(tableView: UITableView, commitEditingStyle editingStyle: UITableViewCellEditingStyle, forRowAtIndexPath indexPath: NSIndexPath) {
if editingStyle == .Delete {
objects.removeAtIndex(indexPath.row)
tableView.deleteRowsAtIndexPaths([indexPath], withRowAnimation: .Fade)
} else if editingStyle == .Insert {
// Create a new instance of the appropriate class, insert it into the array, and add a new row to the table view.
}
}
func filterTheObjects(searchString: String!) {
filteredObjects = objects.filter({(current: String) -> Bool in
let matchString = (current.rangeOfString(searchString) != nil)
return matchString
})
}
func searchDisplayController(controller: UISearchDisplayController!, shouldReloadTableForSearchString searchString: String!) -> Bool {
filterTheObjects(searchString)
return true
}
func searchDisplayController(controller: UISearchDisplayController!, shouldReloadTableForSearchScope searchOption: Int) -> Bool {
filterTheObjects(searchDisplayController.searchBar.text)
return true
}
}
如果我使用dequeueReusableCellWithIdentifier
而不是dequeueReusableCellWithIdentifier:forIndexPath
發生相同的事情。
有什么想法嗎?
確保已將標識符“ Cell”分配給XIB / Storyboard上的某項
我按照上一個問題searchDisplayController,UITableView,Core Data和Swift的說明進行了操作,並用以下代碼替換了override func tableView(tableView: UITableView, cellForRowAtIndexPath indexPath: NSIndexPath) -> UITableViewCell {
override func tableView(tableView: UITableView, cellForRowAtIndexPath indexPath: NSIndexPath) -> UITableViewCell {
var cell = tableView.dequeueReusableCellWithIdentifier("Cell") as? UITableViewCell
if !(cell != nil) {
cell = UITableViewCell(style: UITableViewCellStyle.Value1, reuseIdentifier: "Cell")
}
var object: String
if tableView == searchDisplayController.searchResultsTableView {
object = filteredObjects[indexPath.row] as String
}
else {
object = objects[indexPath.row] as String
}
cell!.textLabel.text = object
cell!.accessoryType = UITableViewCellAccessoryType.DisclosureIndicator
return cell!
}
聲明:本站的技術帖子網頁,遵循CC BY-SA 4.0協議,如果您需要轉載,請注明本站網址或者原文地址。任何問題請咨詢:yoyou2525@163.com.