导致UISearchBar崩溃的自定义UITableViewCell

我有一个UISearchBar设置的tableView。 每当用户开始在search栏中input时,应用程序就会崩溃。 我发现问题是,我正在使用自定义的tableViewCell(当我试图运行应用程序与默认tableViewCell它没有崩溃,并正常工作)。 有想法该怎么解决这个吗? 谢谢。

这是我的代码:

import UIKit import CoreData class KeepTableViewController: UITableViewController, UISearchBarDelegate{ @IBOutlet weak var searchBar: UISearchBar! var filteredQuotes = [AnyObject]() var keptQuotes = [NSManagedObject]() override func viewDidLoad() { super.viewDidLoad() searchBar.delegate = self searchBar.showsScopeBar = true tableView.rowHeight = UITableViewAutomaticDimension getCoreData() searchDisplayController?.searchResultsTableView.registerClass(QuoteyTableViewCell.self, forCellReuseIdentifier: "Cell") } func getCoreData(){ var appDel : AppDelegate = UIApplication.sharedApplication().delegate as AppDelegate var context : NSManagedObjectContext = appDel.managedObjectContext! var req : NSFetchRequest = NSFetchRequest(entityName: "KeptQuotes") var error : NSError? let fetchedResults = context.executeFetchRequest(req, error: &error) as [NSManagedObject]? if let results = fetchedResults { keptQuotes = results }else{ println("Could not fetch \(error), \(error!.userInfo)") } tableView.reloadData() } @IBAction func cancelPressed(sender: AnyObject) { dismissViewControllerAnimated(true, completion: nil) //dismisses the freakin view } override func preferredStatusBarStyle() -> UIStatusBarStyle { return UIStatusBarStyle.LightContent } override func didReceiveMemoryWarning() { super.didReceiveMemoryWarning() // Dispose of any resources that can be recreated. } // MARK: - Table view data source override func numberOfSectionsInTableView(tableView: UITableView) -> Int { // #warning Potentially incomplete method implementation. // Return the number of sections. return 1 } override func tableView(tableView: UITableView, numberOfRowsInSection section: Int) -> Int { // #warning Incomplete method implementation. // Return the number of rows in the section. if tableView == self.searchDisplayController?.searchResultsTableView { return filteredQuotes.count }else{ return keptQuotes.count } } override func tableView(tableView: UITableView, cellForRowAtIndexPath indexPath: NSIndexPath) -> UITableViewCell { let cell : QuoteyTableViewCell = tableView.dequeueReusableCellWithIdentifier("Cell") as QuoteyTableViewCell var entry : NSManagedObject if tableView == self.searchDisplayController!.searchResultsTableView{ entry = filteredQuotes[indexPath.row] as NSManagedObject }else{ entry = keptQuotes[indexPath.row] as NSManagedObject! } cell.authorLabel.text = entry.valueForKey("author") as String! cell.quoteTextLabel.text = entry.valueForKey("quote") as String! cell.quoteTextLabel.sizeToFit() cell.selectionStyle = UITableViewCellSelectionStyle.None return cell } func filterContentForSearchText(searchText: String) { var qs : NSArray = keptQuotes let predicate = NSPredicate(format: "quote contains[c] %@ OR author contains[c] %@", searchText, searchText) filteredQuotes = qs.filteredArrayUsingPredicate(predicate!) println(filteredQuotes) } func searchDisplayController(controller: UISearchDisplayController!, shouldReloadTableForSearchString searchString: String!) -> Bool { self.filterContentForSearchText(searchString) return true } func searchDisplayController(controller: UISearchDisplayController!, shouldReloadTableForSearchScope searchOption: Int) -> Bool { self.filterContentForSearchText(searchDisplayController!.searchBar.text) return true } } 

自定义TableViewCell:

 import UIKit class QuoteyTableViewCell: UITableViewCell { @IBOutlet weak var authorLabel: UILabel! @IBOutlet weak var quoteTextLabel: UILabel! override func awakeFromNib() { super.awakeFromNib() // Initialization code quoteTextLabel.textColor = UIColor(rgba: "#293B50") authorLabel.textColor = UIColor(rgba: "#A4ACB5") } override func setSelected(selected: Bool, animated: Bool) { super.setSelected(selected, animated: animated) // Configure the view for the selected state } } 

我也遇到了同样的问题,也不是很久以前。 我所做的是让我的工作不是直接在原型单元格中放置自定义单元格,而是为它创build了一个单独的.xib文件,将tableViewController的原型单元格设置为0,

 let regularCell = UINib(nibName: "Cell", bundle: nil) self.searchDisplayController!.searchResultsTableView.registerNib(regularCell, forCellReuseIdentifier: "Cell")