>图像调整大小
>图像在滚动上随机播放
这是代码:
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,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 UIKitclass 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}
而我的故事板:
总结以上是内存溢出为你收集整理的ios – 在Swift中的UITableViewCell中设置图像全部内容,希望文章能够帮你解决ios – 在Swift中的UITableViewCell中设置图像所遇到的程序开发问题。
如果觉得内存溢出网站内容还不错,欢迎将内存溢出网站推荐给程序员好友。
欢迎分享,转载请注明来源:内存溢出
评论列表(0条)