0

I am trying to set height of the last tableView row different from the others. Instead of creating a new cell etc, I want to "cut" 10px from the height of this cell. As I have set automatic dimension for height, I can't use a constant value.

This is how it looks like:

tableView.estimatedRowHeight = 54.0
return UITableViewAutomaticDimension

I want something like this, but this does not work:

if isLastLineBla {
    tableView.estimatedRowHeight = 44.0
    return UITableViewAutomaticDimension-10.0
} else { ...
suppe
  • 71
  • 2

3 Answers3

0

I hope you have the dataSource for your tableView and returning dataSource.count in your numberOfRowsAt method.

Now implement the heightForRowAt method to return proper value like below:

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

And do not set the tableView.estimatedRowHeight = 44.0 at every place, the only place you need to set is in your viewDidLoad method.

Santosh
  • 2,900
  • 1
  • 16
  • 16
  • Ok, thanks. Setting it to 44 will not work for me, because there is a label in it which can vary in number of lines / height. So I want to have the height of UITableViewAutomaticDimension but remove 10px of this height. – suppe Feb 22 '17 at 16:01
0

Think you're running into issues with the way a table view works. The table view implements a form of lazy loading so that there aren't too many views in memory.

Try using the table view delegate methods to "report" correct heights based off the cell's index.

https://developer.apple.com/reference/uikit/uitableviewdelegate/1614998-tableview

MarcJames
  • 189
  • 9
  • This is what I am doing right now? (heightForRowAt delegate) – suppe Feb 22 '17 at 16:07
  • In that method, to make the last cell 10px smaller. Can't you call if index.row == collection.count - 1 { return size - 10 } – MarcJames Feb 22 '17 at 16:11
  • Ah sorry, the "isLastLineBla" was just for shorten code. This is exactly what I am trying to do. ;-) But problem is, that I can't do something like: UITableViewAutomaticDimension - 1 – suppe Feb 22 '17 at 16:20
  • 'NSInternalInconsistencyException', reason: 'Invalid row height provided by table delegate. Value must be greater than zero or UITableViewAutomaticDimension.' – suppe Feb 22 '17 at 16:26
  • UITableViewAutomaticDimension printed is "-1.0" – suppe Feb 22 '17 at 16:28
  • Ahh, misunderstood. Don't know the solution. Perhaps since UITableViewAuto... is computed from auto layout -- adjusting a specific height constraint may be work out. Good luck! – MarcJames Feb 22 '17 at 16:31
0

What you have to do is set a flag on your cell, called isLast or something like that. Then, when returning your cells from your table view delegate you should set that flag to true if it is indeed the last cell of your table.

Then, on your cell you should be able to modify your constraints based on that flag, such that the cell has 10 points less then the rest of the cells. I can't tell you exactly what constraint and how to modify it since I have no idea how your cell looks like.

Mihai Fratu
  • 7,579
  • 2
  • 37
  • 63