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/