iOS   发布时间:2022-03-30  发布网站:大佬教程  code.js-code.com
大佬教程收集整理的这篇文章主要介绍了ios – 在Swift中的UITableViewCell中设置图像大佬教程大佬觉得挺不错的,现在分享给大家,也给大家做个参考。
我有一个reddit帖子列表,我想显示缩略图,如果它存在.我有它的功能,但它很麻烦.有两个主要问题:

>图像调整大小
>图像在滚动上随机播放

这是代码

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)
    }
}

解决方法

不幸的是,这似乎是“基本”表格视图单元格的限制.我最终做的是创建一个自定义的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
}

而我的故事板:

大佬总结

以上是大佬教程为你收集整理的ios – 在Swift中的UITableViewCell中设置图像全部内容,希望文章能够帮你解决ios – 在Swift中的UITableViewCell中设置图像所遇到的程序开发问题。

如果觉得大佬教程网站内容还不错,欢迎将大佬教程推荐给程序员好友。

本图文内容来源于网友网络收集整理提供,作为学习参考使用,版权属于原作者。
如您有任何意见或建议可联系处理。小编QQ:384754419,请注明来意。