【发布时间】:2021-11-18 13:17:26
【问题描述】:
为什么在 UITableView cellForRowAt 内部调用时单元格引用变得混乱: (单元格来自
guard let cell = tableView.dequeueReusableCell(withIdentifier: "venuepostcell")as? VenuePostTableViewCell else { return UITableViewCell()}
cell.tag = indexPath.row
sRef.downloadURL { downloadURL, error in
if downloadURL != nil {
print ("=== A \(indexPath.row) \(cell.tag)")
}
}
cell.tag 应该始终等于 indexPath.row。
最初确实如此,但如果我有时刷新 indexPath.row 不等于 cell.tag ???
初始
=== 3 3
=== 0 0
=== 1 1
=== 2 2
=== 4 4
但在刷新时它会恢复为
=== 0 0
=== 1 1
=== 4 0 <-- WRONG
=== 4 4
=== 3 3
=== 3 1 <-- WRONG
=== 2 2
=== 1 3 <-- WRONG
=== 0 4 <-- WRONG
=== 4 0 <-- WRONG
【问题讨论】:
-
细胞被系统回收,
downloadURL是一个异步调用——我并不惊讶它会导致试图混合两者的意外结果。我建议您将下载移出单元格的逻辑并将其放在单独的位置。单元应该只负责显示数据,而不是也对服务器进行异步调用。
标签: swift uitableview google-cloud-firestore