J'avais un problème lorsque mon application plantait et donnait l'erreur Index hors plage, mais après avoir éliminé la fonction pour obtenir imageFiles en arrière-plan, la tableView s'exécute normalement. Je pense que le problème était que deux appels getDataInBackground
le confondaient.Swift Index hors de portée à cellForRowAtIndexPath avec deux appels pour getDataInBackground
Cependant, j'ai encore besoin de télécharger les données pour imageFiles, donc je me demande si quelqu'un a une solution pour comment je peux le faire dans cellForRowAtIndexPath. Toute aide est très appréciée :)
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
}
qu'est-ce que postImageFiles? image url array? –
postImageFiles est un fichier PFFile. donc var postImageFiles = [PFFile]() –