gpt4 book ai didi

ios - Swift 搜索栏和搜索显示 Controller - dequeuereusablecellwithidentifier 抛出异常

转载 作者:行者123 更新时间:2023-11-28 09:20:07 24 4
gpt4 key购买 nike

我在 Xcode 6 beta 6 中创建了一个标准的 iOS 项目,并向 Main.Storyboard 添加了一个搜索栏和搜索显示 Controller 。我还为 searchDisplayController 添加了几个似乎有效的委托(delegate)函数,但是在搜索时弹出以下错误:

Terminating app due to uncaught exception 'NSInternalInconsistencyException', reason: 'unable to dequeue a cell with identifier Cell - must register a nib or a class for the identifier or connect a prototype cell in a storyboard'

这是我的代码!

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 上的某些内容

关于ios - Swift 搜索栏和搜索显示 Controller - dequeuereusablecellwithidentifier 抛出异常,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/25461545/

24 4 0
Copyright 2021 - 2024 cfsdn All Rights Reserved 蜀ICP备2022000587号
广告合作:1813099741@qq.com 6ren.com