大佬教程收集整理的这篇文章主要介绍了ios – 在Swift中的UITableViewCell中设置图像,大佬教程大佬觉得挺不错的,现在分享给大家,也给大家做个参考。
>图像调整大小
>图像在滚动上随机播放
这是代码:
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中@L_944_12@的,不是由我生成的**
编辑:承诺的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) } }
由于代码是如此微不足道,这有点令人沮丧,但我认为从好的方面来看,这意味着它是一个“简单”的解决方案.
我的最终代码:
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 }
而我的故事板:
以上是大佬教程为你收集整理的ios – 在Swift中的UITableViewCell中设置图像全部内容,希望文章能够帮你解决ios – 在Swift中的UITableViewCell中设置图像所遇到的程序开发问题。
如果觉得大佬教程网站内容还不错,欢迎将大佬教程推荐给程序员好友。
本图文内容来源于网友网络收集整理提供,作为学习参考使用,版权属于原作者。
如您有任何意见或建议可联系处理。小编QQ:384754419,请注明来意。