簡體   English   中英

RxDataSources 單元格重新加載動畫無法正常工作

[英]RxDataSources cell reload animations not working properly

我在RxDataSources單元格重新加載RxSwift動畫時遇到了一些問題。 我有一個簡單的表設置,如下所示:

import UIKit
import RxDataSources
import RxCocoa
import RxSwift
import Fakery

class ViewController1: UIViewController {


    @IBOutlet weak var tableView: UITableView!
    let bag = DisposeBag()



    override func viewDidLoad() {
        super.viewDidLoad()
        setupTableView()
    }

    private func setupTableView() {
        tableView.register(UINib(nibName: "TestTableViewCell", bundle: nil), forCellReuseIdentifier: "cell")

        let dataSource = RxTableViewSectionedAnimatedDataSource<SectionOfTestData>(
            animationConfiguration: AnimationConfiguration(insertAnimation: .none, reloadAnimation: .none, deleteAnimation: .none),
            configureCell: { dataSource, tableView, indexPath, element in
                let cell = tableView.dequeueReusableCell(withIdentifier: "cell", for: indexPath) as! TestTableViewCell
                cell.testData = element
                return cell
            })

        someData
            .bind(to: tableView.rx.items(dataSource: dataSource))
            .disposed(by: bag)
    }

    let someData = BehaviorRelay<[SectionOfTestData]>(value: [SectionOfTestData(items: [
        TestData(color: .red, name: "Henry"),
        TestData(color: .blue, name: "Josh")
    ])])

    @IBAction func didTapUpdateButton(_ sender: Any) {
        let colors: [UIColor] = [.blue, .purple, .orange, .red, .brown]


        let items = someData.value.first!.items

        // Add random data when button is tapped
        someData.accept([SectionOfTestData(items: items + [TestData(color: colors.randomElement()!, name: Faker().name.firstName())])])
    }

}

型號:

struct TestData {
    let color: UIColor
    let name: String
}

extension TestData: IdentifiableType, Equatable {
    typealias Identity = Int

    var identity: Identity {
           return Int.random(in: 0..<20000)
    }
}

struct SectionOfTestData {
    var items: [Item]

    var identity: Int {
        return 0
    }
}

extension SectionOfTestData: AnimatableSectionModelType {
    typealias Identity = Int
    typealias Item = TestData

    // Implement default init
    init(original: SectionOfTestData, items: [Item]) {
        self = original
        self.items = items
    }
}

class TestTableViewCell: UITableViewCell {

    @IBOutlet weak var colorView: UIView!
    @IBOutlet weak var nameLabel: UILabel!

    var testData: TestData! {
        didSet {
            colorView.backgroundColor = testData.color
            nameLabel.text = testData.name
        }
    }

}

當點擊按鈕時, BehaviorRelay會更新,表格似乎也在刷新,但是“動畫”總是相同的。 在提供的代碼中,我實際上已將所有動畫類型設置為.none但它仍在執行動畫。 如果我再次嘗試將動畫類型更改為另一種類型,例如.bottom動畫是相同的。 我在這里做錯了什么?

在此處輸入圖片說明

這是重新加載動畫還是插入動畫? 我不知道更新數據時表是否重新加載或插入,我在文檔中找不到任何信息。 任何關於此的指針將不勝感激!

你的問題是:

var identity: Identity {
    return Int.random(in: 0..<20000)
}

RxDataSources 使用標識值來計算變更集。 您已經以每次本質上返回一個新值的方式實現它(除非發生碰撞),因此從框架的角度來看,您總是在刪除所有項目並添加新項目。 您可以通過實施來檢查這一點

decideViewTransition: { _, _, changeset in
    print(changeset)
    return .animated
}

要完成接受的答案:

制作一個uuid並將其傳遞給identity

let id = UUID().uuidString
var identity: String {
   return id
}

然后動畫完美地工作。

暫無
暫無

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

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