Swift3-搜索崩溃表视图的首字母



当我的" else"块的第一行运行时,我会遇到此错误:

无法用凹痕器Phonecell排出单元格 - 必须为标识符注册笔尖或类,或在故事板上连接原型单元格

因此,初始表的加载良好,但是当您尝试进行搜索时,此异常会被抛弃。这才才在我去自定义单元后才开始。这是崩溃的表的CellForrowatIndExpath函数。知道怎么回事?

override func tableView(_ tableView: UITableView, cellForRowAt indexPath: IndexPath) -> UITableViewCell {

    if tableView == self.tableView{
        let cell = self.tableView.dequeueReusableCell(withIdentifier: "PhoneCell", for: indexPath) as! PhoneSearchResultTableViewCell
        cell.nameLabel.text = phones[indexPath.row].name.value
        cell.descriptionLabel.text = phones[indexPath.row].descriptionText.value
        cell.modelLabel.text = phones[indexPath.row].model.value
        return cell
    }else{
        let cell = self.resultsController.tableView.dequeueReusableCell(withIdentifier: "PhoneCell", for: indexPath) as! PhoneSearchResultTableViewCell
        cell.nameLabel.text = filteredPhones[indexPath.row].name.value
        cell.descriptionLabel.text = filteredPhones[indexPath.row].descriptionText.value
        cell.modelLabel.text = filteredPhones[indexPath.row].model.value
        return cell
    }
}

您的 UITableViewController在其故事板中没有一个原型单元格为您的 "PhoneCell",或者如果您以编程方式进行操作,则没有为您正在调用的单元格标识符注册类。

尝试在您的UIViewControllerviewDidLoad或实例化UITableView之前的某个地方添加它:

self.resultsController.tableView.register(PhoneSearchResultTableViewCell.self, forCellReuseIdentifier: "PhoneCell")

似乎您正在使用UITableViewController,而不是单独的UITableView。如果将自定义单元格创建为IB中的原型单元格,请确保重复使用标识符在代码和IB中设置相同。

UITableViewController自动注册原型单元格。

但是,如果您开始单独使用UITableView,则需要使用UINib或自定义单元格的类调用.register方法。

最新更新