我在Xcode 6 beta 6中创建了一个标准的iOS项目,并在Main.Storyboard中添加了一个搜索栏和搜索显示控制器。我还在searchDisplayController的一些委托函数中添加了似乎可行的函数,但是在搜索时会出现以下错误:
由于未捕获的异常终止应用' NSInternalInconsistencyException',原因:'无法将单元格出列 带标识符Cell - 必须注册一个nib或类 标识符或连接故事板中的原型单元'
这是我的代码!
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
,则会发生同样的事情。
有什么想法吗?
答案 0 :(得分:0)
确保您已将标识符“Cell”分配给XIB / Storyboard
上的内容答案 1 :(得分:0)
我按照上一个问题searchDisplayController, UITableView, Core Data and 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!
}