简体   繁体   中英

Dynamically change TableView Cell height

Swift/Objective C - Simple Way to Dynamically change TableView Cell height

In my table view cell break first line and third line, every label will dynamic

在此输入图像描述

Add Constraints to Image View (top, Leading, trailing, Height)

Don't Add Bottom Constraints

在此输入图像描述

Add Constraints to Each label (top, Leading, trailing, Bottom)

在此输入图像描述

Add Constraints to last label (top, Leading, trailing, Bottom)

在此输入图像描述

Set each label

Number of Line = 0

Line Breaks mode = word warp

在此输入图像描述

Table View Datasource and Delegate Methods

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
}

Now All labels are Dynamic

在此输入图像描述

Swift - 3.0

 func tableView(_ tableView: UITableView, heightForRowAt indexPath: IndexPath) -> CGFloat {
     var height:CGFloat = CGFloat()
     if indexPath.row == 0 {
     height = 80
    }
    else if indexPath.row == 1 {
     height = self.view.frame.size.height - 80 - 44 - 64
     print(height)
    }
    return height
   }

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM