sash.tsvet
sash.tsvet

Reputation: 71

Table view "jumps" when I try to scrollToRow with UITableViewAutomaticDimension. The fix also makes it jump

I have the same issue as this one. I have tried to implement a suggested fix, and instead of a table view going the whole way up and then going to the last row again, I got the small jumps up. I need to make the animations smoother. Can it be done with UITableViewAutomaticDimension?

Here is a GIF showing the broken animation


My code is as below:

@objc fileprivate func addNext() {
  guard rowsCount.value < cellModels.count else { return }

  let lastIndexPath = IndexPath(row: rowsCount.value, section: 0)
  rowsCount.value += 1

  CATransaction.begin()
  CATransaction.setCompletionBlock({ () -> Void in
    self.scrollToLastMessage()
  })

  tableView.update {
    tableView.insertRows(at: [lastIndexPath], with: .none)
  }
  CATransaction.commit()
}

func scrollToLastMessage()
{
  let bottomRow = tableView.numberOfRows(inSection: 0) - 1
  let bottomMessageIndex = IndexPath(row: bottomRow, section: 0)

  guard bottomRow >= 0
    else { return }

  CATransaction.begin()
  CATransaction.setCompletionBlock({ () -> Void in

    self.tableView.scrollToRow(at: bottomMessageIndex, at: .bottom, animated: true)
  })

  let contentOffset = tableView.contentOffset.y
  let newContentOffset = CGPoint(x:0, y:contentOffset + 1)  
  tableView.setContentOffset(newContentOffset, animated: false)

  CATransaction.commit()
}


extension UITableView {
  typealias Updates = () -> Void

  func update(_ updates: Updates) {
    beginUpdates()
    updates()
    endUpdates()
  }
}

Thanks in advance.

Upvotes: 3

Views: 734

Answers (1)

sash.tsvet
sash.tsvet

Reputation: 71

I resolved this problem when I added estimated row height for my table view:

var cellHeightsDictionary: [Int:CGFloat] = [:]

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

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

Upvotes: 4

Related Questions