自定义UITableViewCell导致UISearchBar崩溃

4
我有一个带有UISearchBar的tableView。每当用户在搜索栏中开始输入时,应用程序会崩溃。我发现问题是我正在使用自定义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
    }

}

你能分享一下你的项目并使用自定义单元格吗? - Jageen
检查那个单元格是否存在,如果不存在则创建。也许这可以帮助您理解问题。 - user1502383
@Jageen 添加了tableViewCell,不过没什么东西。 - mlevi
@user1502383 我不确定我理解了,你是什么意思? - mlevi
请在此处发布您获得的错误/堆栈跟踪。 - CainaSouza
@Yismo,请分享异常日志描述。 - Vitalii Gozhenko
1个回答

1
我之前也遇到了和你一样的问题。为了解决这个问题,我没有直接在原型单元格中布局我的自定义单元格,而是创建了一个单独的 .xib 文件,并将我的 tableViewController 中的原型单元格设置为 0,然后进行了相应的操作。
let regularCell = UINib(nibName: "Cell", bundle: nil)

self.searchDisplayController!.searchResultsTableView.registerNib(regularCell, forCellReuseIdentifier: "Cell")

网页内容由stack overflow 提供, 点击上面的
可以查看英文原文,
原文链接