【发布时间】:2021-10-10 14:17:51
【问题描述】:
所以在我将这段代码添加到评论下方之后:
DispatchQueue.main.async {
self.tableView.reloadData()
// HERE
let indexPath = IndexPath(row: self.messages.count - 1, section: 0)
self.tableView.scrollToRow(at: indexPath, at: .top, animated: true)
}
滚动时我的单元格混乱(随机生成)。在添加此代码之前,它们的行为符合预期。
我认为这与细胞被重复使用有关,我不知道如何解决这个问题。
有什么解释吗?
cellForRowAt 方法的更多细节:
func tableView(_ tableView: UITableView, cellForRowAt indexPath: IndexPath) -> UITableViewCell {
let cell = tableView.dequeueReusableCell(withIdentifier: K.cellIdentifier, for: indexPath) as! MessageCell
cell.messageText.text = messages[indexPath.row].body
if Auth.auth().currentUser?.email == messages[indexPath.row].sender{
cell.isIncoming = false
} else {
cell.isIncoming = true
}
return cell
}
【问题讨论】:
标签: ios swift uitableview uikit