2
私はthis oneと同じ問題があります。 私は提案されたfixを実装しようとしましたが、テーブルビューの代わりに最後の行に再び行くと、私は小さなジャンプを手に入れました。アニメーションをスムーズにする必要があります。 UITableViewAutomaticDimension
で完了できますか?UITableViewAutomaticDimensionでscrollToRowを実行しようとすると、テーブルビューが「ジャンプ」します。修正はまたそれをジャンプさせる
私のコードは以下の通りです:事前に
@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()
}
}
感謝。