【问题标题】:Swift Index out of Range at cellForRowAtIndexPath with two calls for getDataInBackgroundSwift Index out of Range at cellForRowAtIndexPath 两次调用 getDataInBackground
【发布时间】:2017-04-06 03:38:36
【问题描述】:

我遇到了一个问题,我的应用程序崩溃并给出索引超出范围错误,但在我消除了在后台获取 imageFiles 的功能后,imageFiles[indexPath.row].getDataInBackground tableView 运行正常。我认为问题在于两个getDataInBackgroundcalls 混淆了它。

但是,我仍然需要下载 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?图片 url 数组?
  • postImageFiles 是一个 PFFile。所以 var postImageFiles = [PFFile]()

标签: swift parse-platform tableview indexoutofrangeexception cell-formatting


【解决方案1】:

如果可以使用 URL 加载图像,您可以使用 SDWebImage 库,它会在后台下载图像,并且可以选择设置占位符图像,直到获取原始图像。它还提供了用于缓存图像的选项。 这是图书馆的链接: https://github.com/rs/SDWebImage

【讨论】:

  • 这对 PFFile 有用吗?因为 postImageFiles 是一个包含 [PFFiles]() 的数组,我正在运行查询以从上面的 Parse 下载它们。
  • @FinnWolff Havent 为 PFFiles 尝试了这个库,坦率地说。似乎解析文件有自己的表示,请尝试一下。
  • 我会试一试,但使用 getDataInBackground 绝对有效,我不能两次调用它而不会出错,所以解决这个问题以便我可以下载 imageFiles 和 postImageFiles 的数据是我的主要问题
猜你喜欢
  • 2018-02-08
  • 2013-04-27
  • 1970-01-01
  • 2021-12-27
  • 1970-01-01
  • 1970-01-01
  • 2019-02-23
  • 1970-01-01
  • 2021-12-24
相关资源
最近更新 更多