似乎我的循环似乎将从Feed中获取的所有图像加载到图像视图中,但是只将最后一个图像从Feed设置到所有单元格中我不知道如何停止此操作并抓取每个单元格的图像并设置它。我是swift的新手,我有点跳进编程..继续感兴趣的代码任何帮助表示赞赏
override func tableView(tableView: UITableView, cellForRowAtIndexPath indexPath: NSIndexPath) -> UITableViewCell {
let cell = tableView.dequeueReusableCellWithIdentifier("Cell", forIndexPath: indexPath) as! BlogPost
let blogPost: BlogPost = blogPosts[indexPath.row]
cell.textLabel?.text = blogPost.postTitle
// cell.textLabel?.text = blogPost.postImageLink
// if cell.postImage?.image == nil {
// let cache = ImageLoadingWithCache()
// cache.getImage(cell.postImageLink, imageView: cell.postImage, defaultImage: "IMG_0079")
if cell.postImage?.image == nil {
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)!)!)
self.imageArray.append(image!)
dispatch_async(dispatch_get_main_queue(), { Void in
// set image in main thread
guard indexPath.row < self.imageArray.count else { return }
cell.postImage?.image = self.imageArray[indexPath.row]
})
}
}
博客课程
class BlogPost: UITableViewCell {
@IBOutlet weak var postImage: UIImageView!
var postTitle: String = String()
var postLink: String = String()
var postImageLink: String = String()
@IBOutlet weak var postLabel: NSLayoutConstraint!
override func setSelected(selected: Bool, animated: Bool) {
super.setSelected(selected, animated: animated)
// Configure the view for the selected state
}
}