idocode idocode - 2 years ago 134
Swift Question

Dynamic Table view cell height for iOS 7

I have a view controller containing tableview. Table view consists of custom cell containing single label. I have text of varying length which are to be shown in those cells. The problem i am facing is that, the cells are not getting expanded to the appropriate height. I have tried many solutions present in SO but none of them are working so far.
Here is the code for view controller

class ViewController: UIViewController {

@IBOutlet var tableView: UITableView!

let items = [
"This is the first text",
"This is the first text and this is the second text","now you may be thinking where is the third text?. Well, There was the first text and second text, now here is the third text",
"This is the fourth short and sweet text",
"Slow down you crazy child, you're so ambitious for a juvenile. If you're so smart, tell me why are you still so afraid.","Where's the fire? What's the hurry about. You better cool it off before you burn it out. There's so much to do and so many hours in a day.You got your passion, got your pride. Don't you know that only fools are satisfied. Dream on but don't imagine that they come true. Don't even realise vienna waits for you"]

var prototypeCell:CustomCell!

override func viewDidLoad() {

//setting up tableview
self.tableView.allowsSelection = false
self.tableView.dataSource = self
self.tableView.delegate = self


override func didReceiveMemoryWarning() {
// Dispose of any resources that can be recreated.

func configureCell(cell:CustomCell, forIndexPath indexPath:NSIndexPath)
cell.itemLabel.text = items[indexPath.row]


func configurePrototypeCell()
self.prototypeCell = self.tableView.dequeueReusableCellWithIdentifier("Cell") as! CustomCell


extension ViewController:UITableViewDataSource
func tableView(tableView: UITableView, cellForRowAtIndexPath indexPath: NSIndexPath) -> UITableViewCell {

let cell = tableView.dequeueReusableCellWithIdentifier("Cell") as! CustomCell
configureCell(cell, forIndexPath: indexPath)
return cell


func tableView(tableView: UITableView, numberOfRowsInSection section: Int) -> Int {
return items.count

extension ViewController: UITableViewDelegate

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

self.prototypeCell.itemLabel.text = items[indexPath.row]
self.prototypeCell.itemLabel.preferredMaxLayoutWidth = CGRectGetWidth(self.tableView.frame)


let size = self.prototypeCell.contentView.systemLayoutSizeFittingSize(UILayoutFittingCompressedSize)
let height = size.height
return height


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


CustomCell class is subclass of
. It contains
named itemLabel.

Answer Source

Please try this in custom cell call this from TableviewDelegate method of height to calculate custom cell height.

let size = sizingCell.systemLayoutSizeFittingSize(UILayoutFittingCompressedSize)

Recommended from our users: Dynamic Network Monitoring from WhatsUp Gold from IPSwitch. Free Download