SpaceX
SpaceX

Reputation: 2890

Dynamically adjust the height of the tableview cell based on content - iOS Swift

I am trying to set the row height dynamically based on the content set in the detail text label, by using the below code in Part A.

I am inserting few lines of text into a cell's detail text label as shown below in Part B

I've looked at other similar questions but none have helped.

Can some one please advice how I can adjust the row height dynamically based on the content of the detail text label.

Part A

override func tableView(_ tableView: UITableView, heightForRowAt indexPath: IndexPath) -> CGFloat {
    return UITableViewAutomaticDimension
}

Also tried

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

Part B

 override func tableView(_ tableView: UITableView, cellForRowAt indexPath: IndexPath) -> UITableViewCell {
        let cell = tableView.dequeueReusableCell(withIdentifier: "daysIdentifier", for: indexPath)

        cell.textLabel?.text = days[indexPath.row]

        cell.detailTextLabel?.numberOfLines = 0

        var joinedString            = self.availabilityTimeDict[dayName]?.joined(separator: " \n ")
        cell.detailTextLabel?.text  = joinedString
        return cell
    }

enter image description here

Upvotes: 9

Views: 27228

Answers (3)

user7587085
user7587085

Reputation:

Use custom cell and labels. Set up the constrains for the UILabel. (top, left, bottom, right) Set lines of the UILabel to 0

Add the following code in the viewDidLoad method of the ViewController:

tableView.estimatedRowHeight = 68.0
tableView.rowHeight = UITableView.automaticDimension

// Delegate & data source

override func tableView(tableView: UITableView, heightForRowAtIndexPath indexPath: NSIndexPath) -> CGFloat {
    return UITableView.automaticDimension;
}

Swift 4:

// Delegate & data source

override func tableView(_ tableView: UITableView, heightForRowAt indexPath: IndexPath) -> CGFloat {
     return UITableViewAutomaticDimension
}

Swift 4.2:

// Delegate & data source

override func tableView(_ tableView: UITableView, heightForRowAt indexPath: IndexPath) -> CGFloat {
     return UITableView.automaticDimension
}

Upvotes: 28

Manoj Karki
Manoj Karki

Reputation: 270

  • First setup a custom cell and add a label and set its number of lines to zero and give bottom, top, leading, trailing constraints to cell's content view(dont give the height) also give a custom height to cell in size inspector then in viewDidLoad you just need to do,

tableView.rowHeight = UITableViewAutomaticDimension tableView.estimatedRowHeight = 100.0

Upvotes: 0

Chetan Hedamba
Chetan Hedamba

Reputation: 229

give top,bottom,leading and trailing to your lable inside content of tableview.Use below methods of table view

func tableView(_ tableView: UITableView,heightForRowAt indexPath:IndexPath) -> CGFloat
{
 return UITableViewAutomaticDimension
}

func tableView(_ tableView: UITableView, estimatedHeightForRowAt indexPath: IndexPath) -> CGFloat
{
 return 100
}

Upvotes: 2

Related Questions