RxDataSources单元重载动画无法正常工作

问题描述 投票:0回答:1

我在RxDataSourcesRxSwift单元重新加载动画方面遇到一些问题。我有一个简单的表格设置,如下所示:

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,则动画是相同的。我在这里错了吗?

enter image description here

这是reload动画还是insert动画?我不知道表是在数据更新时重新加载还是插入,我在文档中找不到任何信息。对此,任何指针将不胜感激!

ios swift rx-swift rxdatasources
1个回答
0
投票

您的问题是:

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

RxDataSources使用标识值来计算变更集。您已经以一种基本上每次都返回一个新值的方式实现了它(除非发生冲突),因此对于框架而言,您总是会删除所有项并添加新项。您可以通过执行

进行检查
decideViewTransition: { _, _, changeset in
    print(changeset)
    return .animated
}
© www.soinside.com 2019 - 2024. All rights reserved.