ios - 动态改变TableView Cell高度

标签 ios objective-c iphone swift

Swift/Objective C - 动态更改 TableView 单元格高度的简单方法

在我的表格 View 单元格中,第一行和第三行分开,每个标签都是动态的

enter image description here

最佳答案

向 ImageView 添加约束(顶部、前导、尾随、高度)

不要添加底部约束

enter image description here

为每个标签添加约束(顶部、前导、尾随、底部)

enter image description here

将约束添加到最后一个标签(顶部、前导、尾随、底部)

enter image description here

设置每个标签

行数 = 0

换行模式 = word warp

enter image description here

TableView 数据源和委托(delegate)方法

func tableView(tableView: UITableView, cellForRowAtIndexPath indexPath: NSIndexPath) -> UITableViewCell
{
    let cell = tableView.dequeueReusableCellWithIdentifier("PropertyListCell", forIndexPath: indexPath) as UITableViewCell!

    imgProperty = viewBg.viewWithTag(111) as! RemoteImageView
    lblPropertyName = viewBg.viewWithTag(112) as! UILabel
    lblPrice = viewBg.viewWithTag(113) as! UILabel
    lblAddress = viewBg.viewWithTag(114) as! UILabel
    lblAreaPerSquare = viewBg.viewWithTag(115) as! UILabel

    imgProperty.imageURL = NSURL(string: "Image Url")
    lblPropertyName.text="Jai Maharashtra Apartment"
    lblPrice.text="Rs. 900 - 10000"
    lblAddress.text="411041,Maharashtra Sadan, Pune, Maharashtra , India" // just address changed. 
    lblAreaPerSquare.text="500 Square Meter"

    cell.selectionStyle = UITableViewCellSelectionStyle.None
    return cell
}

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

func tableView(tableView: UITableView, estimatedHeightForRowAtIndexPath indexPath: NSIndexPath) -> CGFloat
{
    return 44.0
}

现在所有标签都是动态

enter image description here

关于ios - 动态改变TableView Cell高度,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/35789560/

相关文章:

ios - 用于设置多步骤注册页面的最佳 ViewController 是什么?

iphone - iPhone 上的搜索栏未修复

iphone - 从不同的 View Controller 调用时,UIView 动画不会触发?

iphone - 如何在 UIScrollView 中复制 UITableView

iphone - 如何确保 MFMailComposeViewController 在正文不为空时不发送电子邮件?

ios - swift 3 错误 : Type 'AVLayerVideoGravity' has no member 'resizeAspectFill'

ios - 在使用图像选择器选择图像时,如果我点击多次,我的 View Controller 也会消失

ios - uiimage 在 uibutton 中更改文本的位置

objective-c - 使用 AFNetworking 通过 HTTP 身份验证获取 JSON

ios - 子类 UITextField 默认在开头添加空格