我遇到了一个问题,我的应用程序崩溃并给出了 Index out of Range 错误,但是在我消除了在后台获取 imageFiles 的功能后,imageFiles[indexPath.row].getDataInBackground
tableView 运行正常。我认为问题在于两个getDataInBackground
电话混淆了它。
但是,我仍然需要下载 imageFiles 的数据,所以我想知道是否有人有解决方案来解决我如何在 cellForRowAtIndexPath 中做到这一点。任何帮助深表感谢:)
override func tableView(_ tableView: UITableView, cellForRowAt indexPath: IndexPath) -> UITableViewCell {
let cell = tableView.dequeueReusableCell(withIdentifier: "userPhotoFeedCell", for: indexPath) as! FeedCell
if PFUser.current()?.objectId != nil {
cell.userUsername.text = PFUser.current()?.username
}
//downloades images and items to populate user post
postImageFiles[indexPath.row].getDataInBackground { (data, error) in
if let userPostImageData = data {
let downloadedUserPostImage = UIImage(data: userPostImageData)
cell.userFeedPhoto.image = downloadedUserPostImage
}
}
/* IF THIS ISN't COMMENTED APP GIVES INDEX OUT OF RANGE CRASH
//downloads images for user profile pic
imageFiles[indexPath.row].getDataInBackground { (data, error) in
if let imageData = data {
let downloadedImage = UIImage(data: imageData)
cell.userProfilePic.image = downloadedImage
}
}
*/
cell.postLocation.text = postLocation[indexPath.row]
cell.postCaption.text = postCaptions[indexPath.row]
cell.userFeedPhoto.image = UIImage(named: "OrangeFuego")
cell.userProfilePic.image = UIImage(named: "usericon.png")
return cell
}