我刚刚开始使用 RxSwift 并面临一些挑战。我已经创建了具有多个部分的表格 View ,并且能够点击并获取详细信息。但是,如果我尝试删除任何特定的单元格,它就不起作用。我不确定我在 RxSwift 中做错了什么。下面是我的代码。
override func viewDidLoad() {
super.viewDidLoad()
// Do any additional setup after loading the view, typically from a nib.
let dataSource = RxTableViewSectionedReloadDataSource<SectionModel<String, User>>(
configureCell: { (_, tv, indexPath, element) in
let cell = tv.dequeueReusableCell(withIdentifier: "Cell", for: indexPath)
cell.textLabel?.text = string
cell.textLabel?.numberOfLines = 0
return cell
},
titleForHeaderInSection: { dataSource, sectionIndex in
return dataSource[sectionIndex].model
}
)
dataSource.canEditRowAtIndexPath = { dataSource, indexPath in
return true
}
viewModel.getUsers()
.bind(to: tableView.rx.items(dataSource: dataSource))
.disposed(by: disposeBag)
tableView.rx
.itemSelected
.map { indexPath in
return (indexPath, dataSource[indexPath])
}
.subscribe(onNext: { pair in
print("Tapped \(pair.1) @ \(pair.0)")
})
.disposed(by: disposeBag)
tableView.rx.itemDeleted
.subscribe{
print($0)
}
.disposed(by: disposeBag)
tableView.rx
.setDelegate(self)
.disposed(by: disposeBag)
}
最佳答案
问题
tableView.rx.itemDeleted
触发包含 indexPath
的事件,删除操作发生的位置。您的数据更改应由您处理。你没有得到任何更新,因为你没有改变任何东西,你只是打印 indexPath
出。
解决方案
由于您使用的是 viewModel.getUsers()
这会返回一个 Observable<[SectionModel<String, User>]>
从你的代码来看。您还应该在 viewModel
上引入一种方法这将用于删除特定 indexPath
处的项目.
为了实现这一点,您需要将元素存储在 BehaviorSubject
中。 .这将保存数据的当前值,并在更新时将新数据发送给订阅它的人。
let sectionListSubject = BehaviorSubject(value: [SectionModel<String, User>]())
当您初始化 viewModel
时,您需要使用数据填充此主题:
sectionListSubject.onNext([
SectionModel(model: "First section", items: [
User(),
User(),
User()
]),
SectionModel(model: "Second section", items: [
User(),
User(),
User()
])
])
然后是你的getUsers()
方法应如下所示:
func getUsers() -> Observable<[SectionModel<String, User>]> {
return sectionListSubject.asObservable()
}
最后一步 viewModel
, 将实现 removeItem(at:)
func removeItem(at indexPath: IndexPath) {
guard var sections = try? sectionListSubject.value() else { return }
// Get the current section from the indexPath
var currentSection = sections[indexPath.section]
// Remove the item from the section at the specified indexPath
currentSection.items.remove(at: indexPath.row)
// Update the section on section list
sections[indexPath.section] = currentSection
// Inform your subject with the new changes
sectionListSubject.onNext(sections)
}
现在在您拥有的代码库上,您只需要更改:
tableView.rx.itemDeleted
.subscribe(onNext: { self.viewModel.removeItem(at: $0) })
.disposed(by: disposeBag)
现在应该可以删除了。
关于iOS:如何使用 RxSwift 删除 TableView 中的单元格,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/52050486/