我有一个reddit帖子列表,我想显示缩略图,如果它存在.我有它的功能,但它很麻烦.有两个主要问题: 图像调整大小 图像在滚动上随机播放 这是代码: override func tableView(tableView: UITableVie
>图像调整大小
>图像在滚动上随机播放
这是代码:
override func tableView(tableView: UITableView, cellForRowAtIndexPath indexPath: NSIndexPath) -> UITableViewCell { let cell = tableView.dequeueReusableCellWithIdentifier("Post", forIndexPath: indexPath) as UITableViewCell let post = swarm.posts[indexPath.row] cell.textLabel!.text = post.title if(post.thumb? != nil && post.thumb! != "self") { cell.imageView!.image = UIImage(named: "first.imageset") var image = self.imageCache[post.thumb!] if(image == nil) { FetchAsync(url: post.thumb!) { data in // code is at bottom, this just drys things up if(data? != nil) { image = UIImage(data: data!) self.imageCache[post.thumb!] = image dispatch_async(dispatch_get_main_queue(), { if let originalCell = tableView.cellForRowAtIndexPath(indexPath) { originalCell.imageView?.image = image originalCell.imageView?.frame = CGRectMake(5,5,35,35) } }) } } } else { dispatch_async(dispatch_get_main_queue(), { if let originalCell = tableView.cellForRowAtIndexPath(indexPath) { originalCell.imageView?.image = image originalCell.imageView?.frame = CGRectMake(5,5,35,35) } }) } } return cell }
这是应用程序加载时 – 看起来一切正常:
然后,如果我点击图像(即使你滚动),它会调整大小:
如果你向上和向下滚动,图片会变得一团糟(看看中间的帖子 – 泛型乐趣):
我究竟做错了什么?
**图片和标题是从reddit中提取的,不是由我生成的**
编辑:承诺的FetchAsync类:
class FetchAsync { var url: String var callback: (NSData?) -> () init(url: String, callback: (NSData?) -> ()) { self.url = url self.callback = callback self.fetch() } func fetch() { var imageRequest: NSURLRequest = NSURLRequest(URL: NSURL(string: self.url)!) NSURLConnection.sendAsynchronousRequest(imageRequest, queue: NSOperationQueue.mainQueue(), completionHandler: { response, data, error in if(error == nil) { self.callback(data) } else { self.callback(nil) } }) callback(nil) } }不幸的是,这似乎是“基本”表格视图单元格的限制.我最终做的是创建一个自定义的TableViewCell.依赖Ray Wenderlich的教程,可以在这里找到: http://www.raywenderlich.com/68112/video-tutorial-table-views-custom-cells
由于代码是如此微不足道,这有点令人沮丧,但我认为从好的方面来看,这意味着它是一个“简单”的解决方案.
我的最终代码:
PostCell.swift(所有脚手架代码)
import UIKit class PostCell: UITableViewCell { @IBOutlet weak var thumb: UIImageView! @IBOutlet weak var title: UILabel! override func awakeFromNib() { super.awakeFromNib() // Initialization code } override func setSelected(selected: Bool, animated: Bool) { super.setSelected(selected, animated: animated) // Configure the view for the selected state } }
PostsController.swift
override func tableView(tableView: UITableView, cellForRowAtIndexPath indexPath: NSIndexPath) -> UITableViewCell { let cell = tableView.dequeueReusableCellWithIdentifier("PostCell", forIndexPath: indexPath) as PostCell let post = swarm.posts[indexPath.row] cell.title!.text = post.title if(post.thumb? != nil && post.thumb! != "self") { cell.thumb!.image = UIImage(named: "first.imageset") cell.thumb!.contentMode = .ScaleAspectFit var image = self.imageCache[post.thumb!] if(image == nil) { FetchAsync(url: post.thumb!) { data in if(data? != nil) { image = UIImage(data: data!) self.imageCache[post.thumb!] = image dispatch_async(dispatch_get_main_queue(), { if let postCell = tableView.cellForRowAtIndexPath(indexPath) as? PostCell { postCell.thumb!.image = image } }) } } } else { dispatch_async(dispatch_get_main_queue(), { if let postCell = tableView.cellForRowAtIndexPath(indexPath) as? PostCell { postCell.thumb!.image = image } }) } } return cell }
而我的故事板: