0

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再次动画是相同的。我在这里做错了什么?

在此处输入图像描述

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

4

2 回答 2

2

你的问题是:

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

RxDataSources 使用标识值来计算变更集。您已经以每次基本上都返回一个新值的方式实现它(除非您遇到冲突),因此从框架的角度来看,您总是删除所有项目并添加新项目。您可以通过实施来检查这一点

decideViewTransition: { _, _, changeset in
    print(changeset)
    return .animated
}
于 2020-01-13T12:48:12.043 回答
1

要完成接受的答案:

制作一个uuid并将其传递给identity

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

然后动画完美地工作。

于 2021-07-23T22:14:06.123 回答