我有一個問題,我的應用程序崩潰,並給索引超出範圍錯誤,但在我取消後臺獲取imageFiles函數後,tableView運行一般。我認爲問題在於兩個getDataInBackground
調用令人困惑。swift索引超出範圍在cellForRowAtIndexPath有兩個調用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
}
什麼是postImageFiles?圖像的網址數組? –
postImageFiles是一個PFFile。所以var postImageFiles = [PFFile]() –