1

내 앱이 충돌하여 인덱스 범위를 벗어난 오류가 발생했지만 백그라운드에서 imageFiles를 가져 오는 기능을 제거한 후에는 imageFiles[indexPath.row].getDataInBackground tableView가 실행됩니다. 정상적으로. 문제는 두 개의 getDataInBackground 호출이 그것을 혼란스럽게 만들었다 고 생각합니다.getDataInBackground에 대한 두 번의 호출로 cellForRowAtIndexPath에서 스위프트 인덱스를 벗어났습니다.

그러나 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 

} 
+1

postImageFiles는 무엇입니까? 이미지 URL 배열? –

+0

postImageFiles는 PFFile입니다. 그래서 var postImageFiles = [PFFile]() –

답변

0

이 URL을 사용하여 이미지를로드 할 수 가능한 경우, 당신은 SDWebImage 라이브러리를 사용 할 수 있습니다, 그것은 백그라운드에서 이미지를 다운로드하는 자리 표시 자 이미지로를 설정하는 옵션이 있습니다 원본 이미지를 가져올 때까지 이미지 캐싱 옵션도 제공합니다. 다음은 라이브러리 링크입니다. https://github.com/rs/SDWebImage

+0

PFFile에서 잘 작동할까요? postImageFiles는 [PFFiles]()를 보유하는 배열이므로 위의 Parse에서 다운로드 할 쿼리를 실행하고 있습니다. –

+0

@FinnWolff Havent는 PFFiles가 솔직 해지기 위해이 라이브러리를 사용했습니다. 파스 파일 자체 표현을 가지고있는 것 같아, 시도 해봐. –

+0

시도해 보지만 getDataInBackground를 사용하면 오류없이 두 번 호출 할 수 없으므로 imageFiles 및 postImageFiles에 대한 데이터를 다운로드 할 수 있도록 주변에서 처리하는 것이 주요 문제입니다. –