我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
再次动画是相同的。我在这里做错了什么?
这是重新加载动画还是插入动画?我不知道数据更新时表是否会重新加载或插入,我在文档中找不到任何信息。对此的任何指示将不胜感激!