4

I have not added any prototype cells but it should work according to the latest iOS 8 tableView. Here is my code

class ViewController: UIViewController,UITableViewDelegate {
@IBOutlet weak var tableView: UITableView!
var tabledata = ["hn, helooo dnsjakdnksandksajndksandkasjnkc sacjkasc jkas kjdjknasjkdnsaklmdlksamxklsamxlksamdklsandk cnsdjdnsklnjnfkdnflasfnlfnkdsnfjkdnfjkdsnjkd njsdkadnaksjdnjkasndsakdnkasdnsalkdn cjkndskasdnsakndksandjksandksajndkj ndsjkadnksalndls;adnklsa;mdas,mcjksanckasdjnklscaskncjks" , "hi i am ishan, helooo dnsjakdnksandksajndksandkasjnkc sacjkasc jkas kjdjknasjkdnsaklmdlksamxklsamxlksamdklsandk cnsdjdnsklnjnfkdnflasfnlfnkdsnfjkdnfjkdsnjkd njsdkadnaksjdnjkasndsakdnkasdnsalkdn cjkndskasdnsakndksandjksandksajndkj ndsjkadnksalndls;adnklsa;mdas,mcjksanckasdjnklscaskncjkssndjkasndjksandkjasndkjasndkjasndkjasndjka ", "a" ]
func numberOfSectionsInTableView(tableView: UITableView) -> Int {
    return 1
}

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

func tableView(tableView: UITableView, cellForRowAtIndexPath indexPath: NSIndexPath) -> UITableViewCell {
   let cell = UITableViewCell(style: UITableViewCellStyle.Default, reuseIdentifier: "Cell")

    cell.textLabel.text = self.tabledata[indexPath.row]

    return cell
}
override func viewDidLoad() {
    super.viewDidLoad()
    self.tableView.estimatedRowHeight = 100.0;
    self.tableView.rowHeight = UITableViewAutomaticDimension;
    tableView.reloadData()
    // Do any additional setup after loading the view, typically from a nib.
}

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


}

I don't see any changes when I remove/add these lines

    self.tableView.estimatedRowHeight = 100.0;
    self.tableView.rowHeight = UITableViewAutomaticDimension;
Adam Young
  • 1,321
  • 4
  • 20
  • 36
  • UITableViewAutomaticDimension will work only if you are using autolayout, so it will determine the height of the cells based on the constraints that you set up in th IB. Check this answer out: http://stackoverflow.com/questions/18746929/using-auto-layout-in-uitableview-for-dynamic-cell-layouts-variable-row-heights – Dániel Nagy Apr 07 '15 at 07:21

4 Answers4

6

For automatic cell sizing to work, you must add layout constraints to the cell subviews that fully describe the vertical size of the views. Without these constraints, your cell's have no way of knowing how big they actually need to be. This is most easily done in a storyboard.

estimatedRowHeight is just a hint to the table view that increases the table load time by deferring geometric calculations for the cells to scroll time (autolayout can be expensive to calculate). Autolayout is still required to tell the table view what size each cell should be.

Also worth noting is that you're not reusing cells in your table view. In your tableView(_:cellForRowAtIndexPath:) method, you should be dequeuing cells instead of creating a new one every time:

func tableView(tableView: UITableView, cellForRowAtAindexPath indexPath: NSIndexPath) -> UITableViewCell {
    let cell = tableView.dequeueReusableCellWithIdentifier("Cell", forIndexPath: indexPath) as! UITableViewCell
    // configure cell...
    return cell
}
Stuart
  • 36,683
  • 19
  • 101
  • 139
2

1 :Add in ViewDidLoad

tableView.estimatedRowHeight = 140 // prototype cell height
tableView.rowHeight = UITableViewAutomaticDimension

2:reload your tableview in viewDidAppear

Important NOTE:to make UITableViewAutomaticDimension work you have to set all left, right, bottom, and top constraints relative to cell container view.

itsji10dra
  • 4,603
  • 3
  • 39
  • 59
Mohsin Qureshi
  • 1,203
  • 2
  • 16
  • 26
-2

It will helps to give Table View Row Height Dynamically

-(CGFloat)tableView:(UITableView *)tableView heightForRowAtIndexPath:(NSIndexPath *)indexPath {

    NSDictionary *dictItinararay=[arrCodes objectAtIndex:indexPath.row];

    CGSize sizeOfName = [[dictItinararay valueForKey:@"Name"] sizeWithFont:[UIFont systemFontOfSize:16] constrainedToSize:CGSizeMake(180, 1000) lineBreakMode:NSLineBreakByWordWrapping];

    if(sizeOfName.height>45) {
        return sizeOfName.height+10;
    }
    else {
        return 45;
    }
}
Dániel Nagy
  • 11,815
  • 9
  • 50
  • 58
Usharao
  • 67
  • 5
-3

you can simple add line to set cell height

cell.textLabel.numberOfLines = 20
Memon Irshad
  • 972
  • 1
  • 8
  • 17