【问题标题】:SDWebImageView using with UIImageView inside a UITableViewCellSDWebImageView 与 UITableViewCell 内的 UIImageView 一起使用
【发布时间】:2026-01-08 12:25:03
【问题描述】:

我正在使用 Facebook Graph API 来获取一些用户的一些帖子,并希望在我的应用中显示它们。为简单起见,以下是我从 Graph API 获取帖子的方式:

FBRequestConnection.startWithGraphPath("/me/home", parameters: ["limit" : 20, "fields" : "id,from,to,message,type,link,story,picture,object_id", "until" : 1424364646], HTTPMethod: "GET", completionHandler: {(FBRequestConnection connection, AnyObject result, NSError error) -> () in
        if let json = result as? NSMutableDictionary {
            self.newsFeedPostsJSON = json
            self.dataArray = self.newsFeedPostsJSON.objectForKey("data") as [FBGraphObject]!
            dispatch_async(dispatch_get_main_queue(), {() in
                self.tableView.reloadData()
            })
       }
})

效果很好(不要介意我在这里设置的参数)。现在,我有我的 self.dataArray,它存储了此请求返回的所有帖子。

在我的 tableView:cellForRowAtIndexPath 中,我准备了我的单元格,这对于每种类型的帖子都是不同的。我有 video、photo、link 类型和正常状态。

因此,如果用户发布链接(或在生活事件中被标记等),我的单元格将如下所示:

现在,有一个名为 SDWebImage (https://github.com/rs/SDWebImage) 的伟大框架,它是 UIImageView 异步下载图像的扩展。

现在,这是那个函数:

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

    var cell : StatusTableViewCell!

    var graphObject = self.dataArray[indexPath.row] as FBGraphObject

    if let type = graphObject.objectForKey("type") as? String {
        switch(type) {
            case "video":
                cell = tableView.dequeueReusableCellWithIdentifier(self.NewsFeedVideoStatusCellIdentifier, forIndexPath: indexPath) as? NewsFeedVideoStatusTableViewCell
                cell.setupProgressView()
                if let picture = graphObject.objectForKey("picture") as String? {
                    var pictureURL = NSURL(string: picture)
                    (cell as NewsFeedVideoStatusTableViewCell).videoThumbImageView.sd_setImageWithURL(pictureURL, completed: {(image: UIImage!, error : NSError!, cacheType : SDImageCacheType!, url) -> Void in
                        (cell as NewsFeedVideoStatusTableViewCell).progressView.stopSpinProgressBackgroundLayer()
                        (cell as NewsFeedVideoStatusTableViewCell).progressView.removeFromSuperview()
                    })
                }
            case "link":
                cell = tableView.dequeueReusableCellWithIdentifier(self.NewsFeedLinkStatusCellIdentifier, forIndexPath: indexPath) as? NewsFeedLinkStatusTableViewCell
                cell.setupProgressView()
            case "photo":
                cell = tableView.dequeueReusableCellWithIdentifier(self.NewsFeedPhotoStatusCellIdentifier, forIndexPath: indexPath) as? NewsFeedPhotoStatusTableViewCell
                cell.setupProgressView()
                if let objectID = graphObject.objectForKey("object_id") as String! {
                    FBRequestConnection.startWithGraphPath("/\(objectID)", parameters: ["fields" : "images"], HTTPMethod: "GET", completionHandler: {(FBRequestConnection connection, AnyObject result, NSError error) -> () in
                        if let imagesArray = (result as NSDictionary).objectForKey("images") as NSArray? {
                            (cell as NewsFeedPhotoStatusTableViewCell).photoImageView.sd_setImageWithURL(self.getImageURLwithWidthAndHeightBetween(imagesArray, lower: 200, upper: 500), completed: {(image: UIImage!, error: NSError!, cacheType: SDImageCacheType!, url) -> Void in
                                (cell as NewsFeedPhotoStatusTableViewCell).progressView.stopSpinProgressBackgroundLayer()
                                (cell as NewsFeedPhotoStatusTableViewCell).progressView.removeFromSuperview()
                            })
                        }
                    })
                }
            default:
                cell = tableView.dequeueReusableCellWithIdentifier(self.NewsFeedNormalStatusCellIdentifier, forIndexPath: indexPath) as? NewsFeedNormalStatusTableViewCell
        }
    }

因此,用于视频的表格视图单元格看起来与查找链接几乎相同。视频有一个小缩略图,它的 URL 在 JSON 对象中提供,键为“picture”。

说到那一行

(cell as NewsFeedVideoStatusTableViewCell).videoThumbImageView.sd_setImageWithURL(pictureURL, completed: {(image: UIImage!, error : NSError!, cacheType : SDImageCacheType!, url) -> Void in

我的应用程序崩溃了。 Xcode 给了我错误“EXC_BAD_ACCESS”,我不知道如何解决。我认为这与完成处理程序和对我的表格视图单元格的引用有关。

谁能帮帮我?

【问题讨论】:

  • 检查videoThumbImageView 是否为nil。如果IBOutlet 没有正确连接到单元原型中,则可能是nil,从而导致问题。
  • 已正确连接,已检查。而且我也没有得到“在展开时意外发现 nil”或类似的东西。 EXC_BAD_ACCESS 看起来此时不允许访问单元格的图像视图。但我不知道如何使它工作?
  • 你并不总是能看到优雅的“unexpected found nil”。很高兴你仍然解决了你的问题。

标签: ios facebook uitableview swift uiimageview


【解决方案1】:

好的,我想通了。 Xcode 向我显示了错误行中的错误。我的代码工作正常。问题是:我无法访问完成处理程序中的单元格图像视图(即这两行:

(cell as NewsFeedVideoStatusTableViewCell).progressView.stopSpinProgressBackgroundLayer()
(cell as NewsFeedVideoStatusTableViewCell).progressView.removeFromSuperview()

如果我将这两行放在主线程上,它甚至都不起作用,总是崩溃。图像下载完成后,必须找到一种解决方法来隐藏我的进度视图。

【讨论】:

  • 优秀。当你登录 (cell as NewsFeedVideoStatusTableViewCell).progressView 时,你得到了你所期望的结果吗?
  • 不。应用程序在向控制台写入内容之前崩溃。
最近更新 更多