Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

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

enter image description here

like image 205
Shrikant Tanwade Avatar asked Mar 04 '16 06:03

Shrikant Tanwade


People also ask

How do you make a table dynamic cell height?

To get dynamic cell heights working properly, you need to create a custom table view cell and set it up with the right Auto Layout constraints. In the project navigator, select the Views group and press Command-N to create a new file in this group.


1 Answers

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

Don't Add Bottom Constraints

enter image description here

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

enter image description here

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

enter image description here

Set each label

Number of Line = 0

Line Breaks mode = word warp

enter image description here

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

enter image description here

like image 116
Shrikant Tanwade Avatar answered Oct 02 '22 18:10

Shrikant Tanwade