(The problem itself is described at the very bottom)

I have a table with variable height cells. In the storyboard, the cell is customized and there everything rests on the constraints. In each cell, the height varies depending on the content (pictures and text of different heights).

For this to work, I added the following code to the table controller class:

override func viewDidLoad() { super.viewDidLoad() api.getUserPosts(afterGetFeed) self.tableView.rowHeight = UITableViewAutomaticDimension self.tableView.estimatedRowHeight = 250.0 } 

Each cell has a button for likes.

 cell.likeBTN.addTarget(self, action: #selector(FeedVC.likeClick(_:)), forControlEvents: .TouchUpInside) 

When a user clicks a button, a command is sent to the server.

  @IBAction func likeClick (sender: UIButton){ let ind = sender.tag //достаю номер строки let postid = allposts[ind].postid! let params = ["cell" : ind] //тут передаётся индекс ячейки в следующий метод, который вызовется после получения ответа сервера api.updateLike(pistid: postid, params: params, afterLikeUpdateFunc: afterUpdateLike) } 

The response is processed and the new number of likes on the button is displayed. I change all these values ​​in the array, and then just call the update method:

  func afterUpdateLike (data: NSData, params: NSDictionary){ let ind : Int = params["cell"] as! Int let indx = NSIndexPath(forRow: ind, inSection: 0) //Тут код парсинга ответа allposts[ind].postliked = true //Вношу в массив (из которого таблица берёт значения) новое состояние лайка для данного поста //---------------------- self.tableView.reloadRowsAtIndexPaths([indx], withRowAnimation: UITableViewRowAnimation.None) } 

Everything is updated as it should. The required cell is triggered, the data is taken from the array and output in this cell. Except for one problem:

Problem: when updating the desired cell, the table will scroll up or down a bit. I googled and found out that the problem is in different heights of the cells. Because of this, a shift occurs. Since the table considers the height according to this value:

self.tableView.estimatedRowHeight = 250.0

This is the average value. If you put more, it will work for some cells. But there are very long cells, so some solution is needed. But I did not find a solution.

    1 answer 1

    Everything is really due to estimatedRowHeight . Example for a table with a fixed amount of data, Swift 3.0:

    1 create an array with values ​​for each cell

     var rowHeights = [CGFloat](repeating: 250.0, count: 300) //собственно, на этапе создания значение роли не играет 

    2 we get the real height of the cell, which should be and change in the array:

     func tableView(_ tableView: UITableView, willDisplay cell: UITableViewCell, forRowAt indexPath: IndexPath) { rowHeights[indexPath.row] = cell.frame.height } 

    3 we palm off necessary values ​​in estimatedHeightForRowAt

     func tableView(_ tableView: UITableView, estimatedHeightForRowAt indexPath: IndexPath) -> CGFloat { return rowHeights[indexPath.row] } 

    4 when updating a cell by index, there is no unnecessary scrolling.