Ios SDWebImageView在UITableViewCell内与UIImageView一起使用
我正在使用Facebook Graph API获取一些用户的帖子,并希望在我的应用程序中显示它们。为了简单起见,以下是我如何从Graph API获取帖子:Ios SDWebImageView在UITableViewCell内与UIImageView一起使用,ios,facebook,uitableview,swift,uiimageview,Ios,Facebook,Uitableview,Swift,Uiimageview,我正在使用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], HTTP
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,它存储了这个请求返回的所有帖子
在我的表格视图中:cellForRowAtIndexPath我准备我的单元格,这对于每种类型的帖子都是不同的。我有类型的视频,照片,链接和正常状态
因此,如果用户发布链接或在生活事件等中被标记,我的手机将如下所示:
现在,有一个称为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
}
}
因此,我的视频tableview单元格看起来与它查找链接几乎相同。视频有一个小缩略图,它的URL在JSON对象中提供,并带有键“picture”
说到那条线
(cell as NewsFeedVideoStatusTableViewCell).videoThumbImageView.sd_setImageWithURL(pictureURL, completed: {(image: UIImage!, error : NSError!, cacheType : SDImageCacheType!, url) -> Void in
我的应用程序崩溃了。Xcode给了我错误EXC_BAD_ACCESS,我不知道如何解决这个问题。我认为这与完成处理程序和对表视图单元格的引用有关
有人能帮我吗?好的,我想出来了。Xcode向我显示了错误行中的错误。我的代码工作正常。问题是:我无法访问完成处理程序中的单元格图像视图,这两行是:
(cell as NewsFeedVideoStatusTableViewCell).progressView.stopSpinProgressBackgroundLayer()
(cell as NewsFeedVideoStatusTableViewCell).progressView.removeFromSuperview()
如果我把这两行放在主线程上,它甚至不起作用,总是崩溃。在图像下载完成后,必须找到一种方法来隐藏我的进度视图。检查videoThumbImageView是否为零。如果IBOutlet没有正确连接到电池原型中,它可能为零,从而导致问题。它连接正确,已经检查过了。我也没有在拆开包装或类似的东西时得到意外发现的零。EXC_BAD_ACCESS此时看起来不允许访问单元格的图像视图。但我不知道如何让它起作用?你不会总是看到优雅的意外发现零。很高兴你解决了你的问题。太好了。当您将单元格记录为NewsFeedVideoStatusTableViewCell.progressView时,您是否得到了预期的结果?没有。应用程序在向控制台写入内容之前崩溃。