1
我正在使用Firebase存儲來存儲用戶的個人資料圖片,我試圖在UITableView中顯示這些個人資料圖片。何時重新加載從Firebase存儲檢索後的UITableView
問題是我找不到在代碼重新加載表視圖的數據一旦圖像已完成檢索的好點。任何時候我調用tableView.reloadData(),它似乎是在圖像下載完成之前,因爲表格單元格的圖像視圖留空。
目前,我迭代通過將顯示在表視圖,並得到他們的個人資料圖片通過調用該功能的用戶:
func loadProfilePicture(userId: String, index: Int) {
// Load the stored image
usersRef.child(userId).observeSingleEvent(of: .value, with: { (snapshot) in
// Load user's profile picture from Firebase Storage if it exists (exists if the user has a profPic URL in the database)
if snapshot.hasChild("profilePictureUrl") {
self.usersStorageRef.child(userId + "/ProfilePicture").data(withMaxSize: 20*1024*1024, completion: {(data, error) in
let storagePicture = UIImage(data:data!)
// Sets the user's profile picture to the loaded image
self.nearbyUsers[index].profilePicture = storagePicture
})
} else {
print("Error -- Loading Profile Picture")
}
DispatchQueue.main.async {
self.tableView.reloadData()
}
})
}
哪裏nearbyUsers
是我表視圖的數據源和我的tableview的cellForRowAt功能如下:
override func tableView(_ tableView: UITableView, cellForRowAt indexPath: IndexPath) -> UITableViewCell {
let cell = tableView.dequeueReusableCell(withIdentifier: "UserCell", for: indexPath) as! UserTableViewCell
let user = nearbyUsers[indexPath.row]
cell.labelUserName.text = user.firstName + " " + user.lastName
print("refreshing")
if user.profilePicture != nil {
cell.imageViewProfilePicture.image = user.profilePicture
}
return cell
}
我已經試過我目前在諸如代碼的一些變化:
- 如果沒有分派到主線程
但無濟於事
所以,我怎麼能重裝呼籲每個用戶
-reloading表視圖的數據loadProfilePicture功能在循環之後重裝表視圖數據我表格視圖的數據,同時確保所有的個人資料圖片已被加載?
謝謝!