tableview 单元格我们如何快速调整单元格的大小以及图像和标签 [英] tableview cell how do we resize cell in swift along with image and label

查看:37
本文介绍了tableview 单元格我们如何快速调整单元格的大小以及图像和标签的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

当我们在 tableview 单元格中有标签和图像时,我们如何根据最后一个单元格(图片中)中的文本快速或标签调整单元格大小,还有更多文本行,但文本正在切断我们如何解决它

when we have a lable and image in tableview cell how do we resize cell in swift or lable as per the text in the Last cell(in picture) there are some more lines of text but text is cutting off how we resolve it

推荐答案

将你的标签高度限制为更大,并将线设为 0.

var pickheight: CGFloat = 0.0

将这一行写在

override func viewDidLoad() {
        super.viewDidLoad()
        tableTrip.rowHeight = UITableViewAutomaticDimension
}

增加tableview cell的方法

func tableView(tableView: UITableView, heightForRowAtIndexPath indexPath: NSIndexPath) -> CGFloat {

     pickheight = self.findHeightForText("Pass your text here like array value as String ", havingWidth: self.view.frame.size.width - 116, andFont: UIFont.systemFontOfSize(14.0)).height

     return "YOUR_DEFALUT_CELL_SIZE" + pickheight
}

查找单元格文本高度的方法..

func findHeightForText(text: String, havingWidth widthValue: CGFloat, andFont font: UIFont) -> CGSize {
        var size = CGSizeZero
        if text.isEmpty == false {
            let frame = text.boundingRectWithSize(CGSizeMake(widthValue, CGFloat.max), options: .UsesLineFragmentOrigin, attributes: [NSFontAttributeName: font], context: nil)
            size = CGSizeMake(frame.size.width, ceil(frame.size.height))
        }
        return size
}

这篇关于tableview 单元格我们如何快速调整单元格的大小以及图像和标签的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆