ios - 试图使同一索引路径的多个单元格出队,这是不允许的。错误

标签 ios swift uitableview

func tableView(_ tableView: UITableView, cellForRowAt indexPath: IndexPath) -> UITableViewCell {
    guard let cell = tableView.dequeueReusableCell(withIdentifier: "CollectionDetailsItem", for: indexPath) as? CharacterCollectionDetailsTableCell else {
        fatalError("Dequeued cell is not an instance of CharacterDetailsTableViewCell class.")
    }
    return cell
}

func tableView(_ tableView: UITableView, willDisplay cell: UITableViewCell, forRowAt indexPath: IndexPath) {
    guard let cell = tableView.dequeueReusableCell(withIdentifier: "CollectionDetailsItem", for: indexPath) as? CharacterCollectionDetailsTableCell else {
        fatalError("Dequeued cell is not an instance of CharacterDetailsTableViewCell class.")
    }
    if let character = character {
        cell.setCollectionViewDataSourceDelegate(dataType: dataTypes[indexPath.row], characterEntity: character)
    }
}

我真的不明白为什么会出现这个错误,有人能帮我看看我做错了什么吗?

最佳答案

由于 tableview 的数据源和委托(delegate)方法实现错误,您会收到错误消息。 dequeueReusableCell 用于在 tableview 中创建可重用的单元格。因此,它应该在 tableview 的 cellForRow dataSource 方法中实现。您在第一种方法中做得很好,但这就是您做错的地方。

func tableView(_ tableView: UITableView, willDisplay cell: UITableViewCell, forRowAt indexPath: IndexPath) {
    guard let cell = tableView.dequeueReusableCell(withIdentifier: "CollectionDetailsItem", for: indexPath) as? CharacterCollectionDetailsTableCell else {
        fatalError("Dequeued cell is not an instance of CharacterDetailsTableViewCell class.")
    }
    if let character = character {
        cell.setCollectionViewDataSourceDelegate(dataType: dataTypes[indexPath.row], characterEntity: character)
    }
}

此方法不是您可以创建单元格的地方,而是您可以根据单元格显示时的要求执行各种任务。所以根据你的要求,它可能是这样的..

func tableView(_ tableView: UITableView, cellForRowAt indexPath: IndexPath) -> UITableViewCell {
    guard let cell = tableView.dequeueReusableCell(withIdentifier: "CollectionDetailsItem", for: indexPath) as? CharacterCollectionDetailsTableCell else {
        fatalError("Dequeued cell is not an instance of CharacterDetailsTableViewCell class.")
    }
    return cell
}

func tableView(_ tableView: UITableView, willDisplay cell: UITableViewCell, forRowAt indexPath: IndexPath) {
    if let cell = cell as? CharacterCollectionDetailsTableCell {
        guard let character = character else {
            return
        }
        cell.setCollectionViewDataSourceDelegate(dataType: dataTypes[indexPath.row], characterEntity: character)
    }
}

谢谢。

关于ios - 试图使同一索引路径的多个单元格出队,这是不允许的。错误,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/49411653/

相关文章:

iphone - UINavigationController:相反方向的弹出 View Controller

ios - 无法在 Xcode 中编译 OpenCV

arrays - 数组的数组快速行为

ios - Swift 中的 Facebook iOS SDK v 4.1.0 和 Cocoapods : cannot import modules

mysql - 如何从 mysql 数据库中获取数据并将其显示在 UITableView 中

ios - Xcode 核心数据终止问题?

ios - 加大节点的攻丝面积。 ios swift

ios - Rx swift : How to create cache for last network response without creating class/struct property?

ios - 快速在 UITABLEVIEW 上向右滑动时添加操作

swift - 从 UITableViewController 中的单元格引用文本字段