0

I've been working around the tableview and got stuck on the following issue.

As far as I understand, the willDisplayCell delegate method should allow me to access the current cell design.

My issue is: it does not work properly. That delegate function runs only five times when trying to display 80 cells. Regardless of anything other than this function, writing a line of code in the delegate function makes it work well.

Here is my code:

func tableView(_ tableView: UITableView, willDisplay cell: UITableViewCell, forRowAt indexPath: IndexPath) {
    if indexPath.row == comment_arr.count - 1 {
        print("end")
        // (1)self.comment_height.constant =  self.comment_table.contentSize.height + 30
    }
    // (2)self.comment_height.constant =  self.comment_table.contentSize.height + 30
}

If the (1) line exists, it doesn't work. But if the (2) line exists, it works well.

How do I get it to work?

Pika Supports Ukraine
  • 3,612
  • 10
  • 26
  • 42
Hyunmin Jung
  • 45
  • 1
  • 6
  • try an alternative method to detect reaching the tableview's end.. I don't recommend using "willDisplayCell", even if it worked with you though, for many reasons.. check this https://stackoverflow.com/questions/39015228/detect-when-uitableview-has-scrolled-to-the-bottom – Osa Feb 03 '19 at 17:42
  • Does `print("end")` ever print anything? If not, the logic in your if statement may be the issue. – forgot Feb 03 '19 at 20:52

2 Answers2

1

First get the number of rows in section. Then find the last item

let totalRow = tableView.numberOfRows(inSection: indexPath.section)
        if(indexPath.row == totalRow - 1)
        {
           print("end")
            return
        }
  • 1
    Okay, Your answer is helpful, I solve this problem by here https://medium.com/@dushyant_db/swift-4-recipe-self-sizing-table-view-2635ac3df8ab – Hyunmin Jung Feb 14 '19 at 06:58
-1
tableView(_ tableView: UITableView, 
         heightForRowAt indexPath: IndexPath) -> CGFloat

is called before willDisplayCell

the topic was covered in full and detailed way here: TableView methods

Maxim Aba
  • 79
  • 6