【问题标题】:Crash with PFQueryTableViewController and UISearchBarPFQueryTableViewController 和 UISearchBar 崩溃
【发布时间】:2015-12-08 10:42:38
【问题描述】:

崩溃消息

UISearchResultsTableView dequeueReusableCellWithIdentifier:forIndexPath:], /BuildRoot/Library/Caches/com.apple.xbs/Sources/UIKit_Sim/UIKit-3512.29.5/UITableView.m:6547 2015-12-08 16:29:01.851 EventAppTest[51390:3508575] *** 由于未捕获的异常“NSInternalInconsistencyException”而终止应用程序,原因:“无法使具有标识符 EventCell 的单元格出列 - 必须为标识符或连接故事板中的原型单元'

但它会获取所有对象,所以单元格就在那里,为什么它只有在点击搜索栏后才会崩溃?

问题出在哪里?

    import UIKit
import Parse
import ParseUI

class EventListVC: PFQueryTableViewController,UISearchBarDelegate
{
    @IBOutlet var searchBar: UISearchBar!
    var searchString = ""
    var searchInProgress = false

    override func viewDidLoad() {
        super.viewDidLoad()

        // Do any additional setup after loading the view.
        tableView.estimatedRowHeight = tableView.rowHeight
        tableView.rowHeight = UITableViewAutomaticDimension
        searchBar.delegate = self

    }

override func queryForTable() -> PFQuery {
        let query = PFQuery(className: "Event")

        if searchInProgress {
            query.whereKey("eventTitle", containsString: searchString)
        }
        if self.objects!.count == 0 {

            query.cachePolicy = PFCachePolicy.CacheThenNetwork
        }

        query.orderByAscending("createdAt")
        return query

    }

   override func tableView(tableView: UITableView, cellForRowAtIndexPath indexPath: NSIndexPath, object: PFObject?) -> PFTableViewCell? {

        // Configure the cell


        var cell = tableView.dequeueReusableCellWithIdentifier("EventCell", forIndexPath: indexPath) as? EventTableCell

        if cell == nil {
            cell = EventTableCell(style: UITableViewCellStyle.Default, reuseIdentifier: "EventCell")
        }

        cell!.titleLabel.text = object?.objectForKey("eventTitle") as? String



        return cell

    }

    func searchBar(searchBar: UISearchBar, textDidChange searchText: String) {
        searchString = searchText
        searchInProgress = true
        self.loadObjects()
        searchInProgress = false
    }


    func searchBarTextDidBeginEditing(searchBar: UISearchBar) {
        searchBar.showsCancelButton = true
    }

    func searchBarSearchButtonClicked(searchBar: UISearchBar) {
        searchString = searchBar.text!
        self.loadObjects()
    }

    func searchBarCancelButtonClicked(searchBar: UISearchBar) {
        searchBar.text = nil
        searchString = ""
        self.loadObjects()
        searchBar.resignFirstResponder()
    }

【问题讨论】:

  • 您是否为您的单元制作了自定义 nib 文件?
  • 不,我没有,我只是用 PFTableViewCell 制作了故事板中的单元格
  • 好的,那么您是否在情节提要中指定了单元格标识符?
  • 我认为 >> query.whereKey(col name here ?, containsString: searchString) >>> 中的问题对吗?
  • 根据您上面发布的错误,它说它无法找到标识符为“EventCell”的单元格

标签: ios swift uitableview parse-platform uisearchbar


【解决方案1】:

知道了:)

从这里

 var cell = tableView.dequeueReusableCellWithIdentifier("EventCell", forIndexPath: indexPath) as? EventTableCell

到这里

 var cell = tableView.dequeueReusableCellWithIdentifier("EventCell") as? EventTableCell

【讨论】:

    【解决方案2】:

    根据您收到的错误,我认为您错过了指定单元标识符。
    您可以在 Xcode 右侧的属性窗口中找到它
    尝试将这张图片中的“标识符”更改为下一张图片的值。


    您将在此处指定标识符



    希望这会有所帮助

    【讨论】:

    • 谢谢,但它在故事板中,否则会在它开始之前崩溃
    • 嗯,你能发布 loadObjects() 函数吗?
    • 问题可能出在搜索栏中,但是当编译器无法加载单元格时会出现此错误(意味着单元格有一些错误)
    • 没错,我猜它想获取与输入的搜索文本匹配的单元格,但未声明该单元格!
    • 好的,如果你想执行搜索功能,试试这个。 github.com/marmikshah/Auto-Complete 它是一个带有工作搜索的简单项目
    猜你喜欢
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    相关资源
    最近更新 更多