tableview单元格如何在swift中调整单元格以及图像和标签 [英] tableview cell how do we resize cell in swift along with image and label

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

问题描述

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

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单元格的方法。

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
}

为单元格查找文字高度的方法..
< img src =https://i.stack.imgur.com/EXkyL.pngalt =我的标签约束......>

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单元格如何在swift中调整单元格以及图像和标签的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

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