gpt4 book ai didi

ios - RxDataSources 单元重新加载动画无法正常工作

转载 作者:行者123 更新时间:2023-11-28 23:20:26 26 4
gpt4 key购买 nike

RxSwiftRxDataSources 单元重新加载动画有一些问题。我有一个像这样的简单表格设置:

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

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

最佳答案

你的问题是:

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

RxDataSources 使用标识值来计算变更集。您已经以一种基本上每次都返回一个新值(除非发生碰撞)的方式实现了它,因此从框架的角度来看,您总是在删除所有项目并添加新项目。您可以通过实现

来检查这一点
decideViewTransition: { _, _, changeset in
print(changeset)
return .animated
}

关于ios - RxDataSources 单元重新加载动画无法正常工作,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/59715153/

26 4 0
Copyright 2021 - 2024 cfsdn All Rights Reserved 蜀ICP备2022000587号
广告合作:1813099741@qq.com 6ren.com