Yogesh Patel
Yogesh Patel

Reputation: 2017

UITableViewCell Size is not expanding at runtime .?

here is a storyboard image. i have take a dynamic tableviewcell I have tried with drag and drop UITableViewCell and change row height from the storyboard, it cannot expanded cell height at runtime.

In Xcode 9.0.1 and Swift 4, I am facing issues regarding cell height not increasing at runtime.so, pls help me for solving this issue.

enter image description here

enter image description here

Upvotes: 3

Views: 6292

Answers (2)

Krunal
Krunal

Reputation: 79636

To set automatic dimension for row height & estimated row height, ensure following steps to make, auto dimension effective for cell/row height layout.

  • Assign and implement dataSource and delegate
  • Assign UITableViewAutomaticDimension to rowHeight & estimatedRowHeight
  • Implement delegate/dataSource methods (i.e. heightForRowAt and return a value UITableViewAutomaticDimension to it)

-

@IBOutlet weak var table: UITableView!

override func viewDidLoad() {
    super.viewDidLoad()

    // Don't forget to set dataSource and delegate for table
    table.dataSource = self
    table.delegate = self

    // Set automatic dimensions for row height
    table.rowHeight = UITableViewAutomaticDimension
    table.estimatedRowHeight = UITableViewAutomaticDimension
}



// UITableViewAutomaticDimension calculates height of label contents/text
func tableView(_ tableView: UITableView, heightForRowAt indexPath: IndexPath) -> CGFloat {
    return UITableViewAutomaticDimension
}

For label instance in UITableviewCell

  • Set number of lines = 0 (& line break mode = truncate tail)
  • Set all constraints (top, bottom, right left) with respect to its superview/ cell container.
  • Optional: Set minimum height for label, if you want minimum vertical area covered by label, even if there is no data.

enter image description here

Upvotes: 8

PGDev
PGDev

Reputation: 24341

Try this:

1. Implement UITableViewDelegate methods:

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

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

2. Apply proper constraints to your UITableViewCell so that it can correctly calculate its own height. It is must for right UITableViewCell UI to appear. Any issue in autolayout might cause your cell UI to behave in an unexpected manner.

Upvotes: 2

Related Questions