我很难获得tableviewcell的每张图片来显示我博客的缩略图。链接是有效的,因为我打印到屏幕,以确保它在这里工作是导致我的大脑动脉瘤的代码块
override func tableView(tableView: UITableView, cellForRowAtIndexPath indexPath: NSIndexPath) -> UITableViewCell {
let cell = tableView.dequeueReusableCellWithIdentifier("Cell", forIndexPath: indexPath)
let blogPost: BlogPost = blogPosts[indexPath.row]
//cell.textLabel?.text = blogPost.postTitle
postImageLink = blogPost.postImageLink
cell.textLabel?.text = blogPost.postImageLink
if postImageLink != "" && cell.imageView?.image != nil {
dispatch_async(dispatch_get_global_queue( DISPATCH_QUEUE_PRIORITY_DEFAULT, 0)) {
cell.imageView?.image = UIImage(data: NSData(contentsOfURL: NSURL(string:self.postImageLink)!)!)
}
} else {
cell.imageView?.image = UIImage(named: "IMG_0079")
}
return cell
}
我将文本标签设置为postimagelink,以确保它正在解析正确的代码以供将来使用。 要清楚问题是我的博客的缩略图不会加载。只有预设图像才能显示缩略图..postImageLink是我解析的每个缩略图的唯一URL。 (是的,我知道我可以写一个解析器,但不能解决这个问题=(..) 在我将笔记本电脑从屋顶上移开之前,感谢任何帮助! here is image
-rookieprogrammer
答案 0 :(得分:0)
尝试制作两个块,以检索和设置图像
dispatch_async(dispatch_get_global_queue(DISPATCH_QUEUE_PRIORITY_DEFAULT, 0)) {
// retrieve image from url
let image = UIImage(data: NSData(contentsOfURL: NSURL(string:self.postImageLink)!)!)
dispatch_async(dispatch_get_main_queue(), { Void in
// set image in main thread
cell.imageView?.image = image
})
}
可以阅读更多here
答案 1 :(得分:0)
您的代码正常运行。它应该在main_queue,UI主线程中进行,以编辑任何UI对象。
dispatch_async(dispatch_get_main_queue()) {
() -> Void in
// retrieve image from url
let image = UIImage(data: NSData(contentsOfURL:NSURL(string:self.postImageLink)!)!)
cell.imageView?.image = image
}