簡體   English   中英

UITableView重新加載動畫無法正常工作

[英]UITableView reload animation doesn't work properly

我有一個表格視圖,可在點擊按鈕時通過動畫重新加載數據。 效果很好,直到有一個帶有文本的標簽為止。 我使用此代碼為一個按鈕重新加載動畫數據(我只有一個部分):

 tableView.reloadSections(IndexSet(integersIn: 0..<tableView.numberOfSections), with: UITableViewRowAnimation.right)

這是另一個按鈕:

 tableView.reloadSections(IndexSet(integersIn: 0..<tableView.numberOfSections), with: UITableViewRowAnimation.left)

並且在表格視圖的頂部可以正常工作,但在中間或最后滾動。 gif的鏈接-https: //giphy.com/gifs/l0DAHUyzm7BMGnKrm/html5

然后,我添加了以下代碼以重新加載動畫:

        let offset = tableView.contentOffset 
        tableView.beginUpdates()
        tableView.reloadSections(IndexSet(integersIn: 0..<tableView.numberOfSections), with: UITableViewRowAnimation.left)
        tableView.endUpdates()
        tableView.layer.removeAllAnimations()
        tableView.setContentOffset(offset, animated: false)

.right動畫的相同代碼。 它修復了滾動,但增加了另一個問題。 再次在表格視圖上工作還可以,但是然后...請觀看gif。 gif的鏈接-https: //giphy.com/gifs/xT1R9Gfaa2po6dMf2U/html5

我正在使用測試數據填充表格視圖,而不是獲取其他內容。 希望有所幫助,謝謝

編輯:我發現如果我從代碼動畫設置標准單元格高度很好,僅在這種情況下,它可以工作:

    func tableView(_ tableView: UITableView, heightForRowAt indexPath: IndexPath) -> CGFloat {
    return 44.0
}

我發現了問題。 問題出在單元格高度,動畫開始於44.0的估計單元格高度,而我的單元格高度是117.0。 所以這段代碼解決了我的問題:

    func tableView(_ tableView: UITableView, heightForRowAt indexPath: IndexPath) -> CGFloat {
    return 117.0
}
func tableView(_ tableView: UITableView, estimatedHeightForRowAt indexPath: IndexPath) -> CGFloat {
    return 117.0
}

暫無
暫無

聲明:本站的技術帖子網頁,遵循CC BY-SA 4.0協議,如果您需要轉載,請注明本站網址或者原文地址。任何問題請咨詢:yoyou2525@163.com.

 
粵ICP備18138465號  © 2020-2024 STACKOOM.COM