2

I have table view to display comments of users

I need to make height of each row dynamic depending on content height

i searched for it and i found

heightForRowAtIndexPath Method

but it's not working or i don't know how to use it !

here's my code :

func numberOfSectionsInTableView(tableView: UITableView) -> Int {
    return 1
}

func tableView(tableView: UITableView, cellForRowAtIndexPath indexPath: NSIndexPath) -> UITableViewCell {
    let cell = tableView.dequeueReusableCellWithIdentifier("cell", forIndexPath: indexPath)
   let username = cell.viewWithTag(1) as! UILabel

    let comment = cell.viewWithTag(2) as! UITextView
    username.text = usernames[indexPath.row]

    comment.text = comments[indexPath.row]

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

func tableView(tableView: UITableView, heightForRowAtIndexPath indexPath: NSIndexPath) -> CGFloat {
    return UITableViewAutomaticDimension;
}
rmaddy
  • 314,917
  • 42
  • 532
  • 579
Muhammed
  • 584
  • 1
  • 11
  • 24
  • Perhaps this post may help http://stackoverflow.com/questions/26136946/ios8-possible-to-use-tableview-rowheight-uitableviewautomaticdimension-for Or this one http://stackoverflow.com/questions/30299319/why-uitableviewautomaticdimension-not-working – hawkstrider Mar 10 '16 at 19:43

1 Answers1

7

You have not implemented the heightForRowAtIndexPath method correctly. You should read up on self sizing table view cells as it will do what you want.

Basically, to use self sizing cells, for the UITableView you will set an estimated row height, and also set the rowHeight to a value of UITableViewAutomaticDimension (or UITableView.automaticDimension in Swift 4.2 or later) .

before Swift 4.2:

tableView.estimatedRowHeight = 85.0
tableview.rowHeight = UITableViewAutomaticDimension

Swift 4.2:

tableView.estimatedRowHeight = 85.0
tableView.rowHeight = UITableView.automaticDimension

Set the estimated row height value to a value that closely approximates the rough average height of all the cells. This helps iOS have an idea of how big the full UIScrollView content is.

Also, with self sizing cells, you will not implement heightForRowAtIndexPath at all. Each cell height is obtained from the constraints within each cell.

For a good guide on self sizing cells, check out this tutorial.

If you don't want to do self sizing cells, you can implement heightForRowAtIndexPath, but you need to return the correct height for each cell. That logic will be up to you to determine based on the indexPath parameter. But you need to make sure that you return the height for each cell (specified by indexPath) in pixels (logical pixels).

wottle
  • 13,095
  • 4
  • 27
  • 68
  • i removed heightForRowIndexPath and used self.myTable.estimatedRowHeight = 160 self.myTable.rowHeight = UITableViewAutomaticDimension – Muhammed Mar 13 '16 at 13:23
  • 1
    You need to make sure your cells are all set up properly with auto layout . Without them sizing properly, it will not work. – wottle Mar 14 '16 at 15:01
  • you mean constraints ? – Muhammed Mar 24 '16 at 16:15
  • Yes, the autolayout constraints need to be set properly in order for the self sizing cells to know how big each should be. – wottle Mar 24 '16 at 18:48