[英]indexPath.row starts at 1 instead of 0 after reload
我的数据库中有 3 个项目,我在 viewDidLoad() 中调用 tableView.reloadData() 单元格是从 FireStore 数据库中检索的。 但是即使在 cellForRowAt 中的 print(indexPath.row) 之前,数据库也会从数据库中返回 3 个项目。 我有一个喜欢 function 并且每次我有超过 2 个项目时它都会中断,我怀疑是因为这个。 慢速计算机会导致此问题吗?
db 中有 3 个项目的 print(indexPath.row) 返回:
0 1 2 1 2
有 2 件物品:
0 1 1
//cellForRowAt
func tableView(_ tableView: UITableView, cellForRowAt indexPath: IndexPath) -> UITableViewCell {
let cell = tableView.dequeueReusableCell(withIdentifier: "PostCell", for: indexPath) as! PostCell
let p = postList[indexPath.row]
print("\(indexPath.row)")
// print("\(p.postId)")
cell.recipeName.text = p.recipeName
profileDataManager.loadProfile(p.username){
user in
self.userList = user
for i in self.userList{
cell.userName.text = i.displayName
}
}
cell.CLHLabel.text = "\(p.likes) likes, 10 comments, \(p.healthy) users find this healthy"
cell.tagsLabel.text = "\(p.tagBudget), \(p.tagPrep), \(p.tagStyle)"
cell.postID = p.postId
cell.postItem = p
cell.delegate = self
let imageRef = Storage.storage().reference(withPath: p.postImage)
imageRef.getData(maxSize: 4 * 1024 * 1024) { [weak self] (data, error) in
if let error = error {
print("Error downloading image: \(error.localizedDescription)")
return
}
if let data = data {
cell.postImage.image = UIImage(data: data)
}
}
cell.loadCell()
return cell
}
//numberOfRowsInSection
func tableView(_ tableView: UITableView, numberOfRowsInSection section: Int) -> Int {
return postList.count
}
找出问题所在,我的 tableView 有一个自动增长表 function 看起来像这样
class IntrinsicPostTableView: UITableView {
override var contentSize:CGSize {
didSet {
self.invalidateIntrinsicContentSize()
}
}
override var intrinsicContentSize: CGSize {
self.layoutIfNeeded()
return CGSize(width: UIView.noIntrinsicMetric, height: contentSize.height)
}
}
由于某种原因,这导致我的 indexPath.row 从 1 而不是 0 开始
声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.