2017-11-14 6 views
2

私はthis oneと同じ問題があります。 私は提案されたfixを実装しようとしましたが、テーブルビューの代わりに最後の行に再び行くと、私は小さなジャンプを手に入れました。アニメーションをスムーズにする必要があります。 UITableViewAutomaticDimensionで完了できますか?UITableViewAutomaticDimensionでscrollToRowを実行しようとすると、テーブルビューが「ジャンプ」します。修正はまたそれをジャンプさせる

Here is a GIF showing the broken animation


私のコードは以下の通りです:事前に

@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() 
    } 
} 

感謝。

答えて

1

私は、テーブルビューの推定行の高さを追加したとき、私は、この問題を解決:

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 
} 
関連する問題