ios - 使用 S3 下载功能异步快速更新 tableview

标签 ios swift uitableview asynchronous uiimage

我有一个异步问题 - 我有一个从 S3 加载图像的函数,将它们存储到 UIImages 数组中(此处称为 Images)

我还有一个表格 View ,可以从 firebase 获取的数据加载其单元格,我的问题是,异步完成加载后如何更新单元格图像?

我还担心图像队列与indexPath.row不完全匹配,因为某些图像可能比其他图像加载得更快。

 func download(key:String, myindex:NSIndexPath, myrow:Int) -> NSString {
    let path:NSString = NSTemporaryDirectory().stringByAppendingString("image.jpg")
    let url:NSURL = NSURL(fileURLWithPath: path as String)

    //    let downloadingFilePath = downloadingFileURL.path!
    let downloadRequest = AWSS3TransferManagerDownloadRequest()
    downloadRequest.bucket = "witnesstest/" + rootref.authData.uid
    downloadRequest.key = key
    downloadRequest.downloadingFileURL = url

    switch (downloadRequest.state) {
    case .NotStarted, .Paused:
        let transferManager = AWSS3TransferManager.defaultS3TransferManager()
        transferManager.download(downloadRequest).continueWithBlock({ (task) -> AnyObject! in
            if let error = task.error {
                if error.domain == AWSS3TransferManagerErrorDomain as String
                    && AWSS3TransferManagerErrorType(rawValue: error.code) == AWSS3TransferManagerErrorType.Paused {
                        print("Download paused.")
                } else {
                    print("download failed: [\(error)]")
                }
            } else if let exception = task.exception {
                print("download failed: [\(exception)]")
            } else {
                dispatch_async(dispatch_get_main_queue(), { () -> Void in
                    let tempimage:UIImage = UIImage(contentsOfFile: path as String)!
                    print("dl ok")
                    self.Images.append(tempimage)

            })
            }
            return nil
        })

        break
    default:
        break
    }
    return path
}

和单元格:

func tableView(tableView: UITableView, cellForRowAtIndexPath indexPath: NSIndexPath) -> UITableViewCell {

    let cell: MainWitnessTableViewCell = self.tableView.dequeueReusableCellWithIdentifier("RIcell") as! MainWitnessTableViewCell

        // populate the cell
        let postitem = posts[indexPath.row]
        cell.postOwner.text = postitem.author
        cell.postContent.text = postitem.content
        cell.postDate.text = postitem.createon

        let myindex = indexPath
        let myrow = indexPath.row




       // cell.cellImage.image = Images[indexPath.row] // returns array index out of range

     //   download(postitem.imagekey, myindex: myindex, myrow: myrow)

    return cell
}

最佳答案

您可以在下载图像后设置 imageView 的图像,同时设置 postitem 的 image 属性(假设您添加了一个)。我很难理解你的下载方法所做的一切,但我认为你想要的要点是这样的:

func tableView(tableView: UITableView, cellForRowAtIndexPath indexPath: NSIndexPath) -> UITableViewCell {

    let cell: MainWitnessTableViewCell = self.tableView.dequeueReusableCellWithIdentifier("RIcell") as! MainWitnessTableViewCell

        // populate the cell
        let postitem = posts[indexPath.row]
        cell.postOwner.text = postitem.author
        cell.postContent.text = postitem.content
        cell.postDate.text = postitem.createon

        //postitem should also have a imageURL property or you should have some way of getting the image url.
        if post item.image == nil{
            dispatch_queue_t imageFetchQ = dispatch_queue_create("image fetcher", NULL)
            dispatch_async(imageFetchQ, ^{

               let imageData = NSData(contentsOfURL: postitem.imageURL)
               let image = UIImage(data: imageData)
               postitem.image = image
               dispatch_async(dispatch_get_main_queue(), ^{
                   //the UITableViewCell may have been dequeued and reused so check if the cell for the indexPath != nil
                   if tableView.cellForRowAtIndexPath(indexPath) != nil{
                       cell.imageView.image = image
                   }
    })

})





    return cell
}

关于ios - 使用 S3 下载功能异步快速更新 tableview,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/35465356/

相关文章:

ios - 内部选项卡栏 View Controller 导航栏更改无法从 Storyboard 中工作

ios - 无法快速设置 CustomCells 的高度

swift - 检查是否在 UIWebView 中按下了链接

Swift NSMenuItem 检测 Shift 单击

swift - 编辑项目中 csv 文件中的值会引发 "index out of range"错误

ios - didSelectRowAt indexPath 播放音频

ios - 删除 Storyboard上表格 View 单元格的分隔线

ios - 阻止 Speechkit 使 iOS 9 设备崩溃

iphone - 如何制作一个管理两个 subview Controller 的父 View Controller

ios - 尝试从 NSMutableArray 填充 UITableView 时应用程序崩溃