uitableview 以错误的顺序重新加载数组

问题描述 投票:0回答:0

假设我将数组 users = [Sarah, Lara] 存储在我的 allUsers 数组中。我希望我的表视图以准确的顺序返回用户。但是在我的 cellForRowAt 中发生了这种情况: 在 indexPath.row = 0 时返回用户“sarah”,在 indexPath.row = 1 时返回用户“lara”。然后,如果我重新加载页面,它会发生变化,indexPath.row = 0 返回用户“lara”,indexPath.row = 1 返回。用户“sarah”。

在我的 UI 表视图中:

 private func downloadLikes() { 
     ProgressHUD.show()
        FirebaseListener.shared.downloadUserLikess { (likedUsers) in
            var likedIds: [String] = []
            for liked in likedUsers {
                likedIds.append(liked.userId)
            }
            if likedIds.count > 0 {
                FirebaseListener.shared.downloadUsersFromFirebase(withIds: likedIds) { (allUsers) in                        
                    ProgressHUD.dismiss()

                    self.allUsers = allUsers                       
                   
                    DispatchQueue.main.async {                            
                        self.tableView.reloadData()
                    }
                }
            } else {
                ProgressHUD.dismiss()
            }
        }
}

func tableView(_ tableView: UITableView, numberOfRowsInSection section: Int) -> Int {
    return allUsers.count
}

func tableView(_ tableView: UITableView, cellForRowAt indexPath: IndexPath) -> UITableViewCell {

    let cell = tableView.dequeueReusableCell(withIdentifier: "LikeCell", for: indexPath) as! LikeTableViewCell
    print(indexPath.row)
    print(allUsers[indexPath.row].username)
    cell.setupCell(user: allUsers[indexPath.row])
    return cell
}

在我的课上 LikeTableViewCell:

private func setAvatar(avatarLink: String) {
    
    FileStorage.downloadImage(imageUrl: avatarLink) { (avatarImage) in
        let placeholder = "mPlaceholder"
        self.avatarImageView.image = avatarImage?.circleMasked ?? UIImage(named: placeholder)?.circleMasked
    }
}

func setupCell(user: FUser) {
    nameLabel.text = user.username
    setAvatar(avatarLink: user.avatarLink) }
arrays swift firebase uitableview swiftui
© www.soinside.com 2019 - 2024. All rights reserved.