ios - 如何允许编辑由 RxDataSources 支持的表视图?
问题描述
我正在构建一个由 RxDataSources 支持的表视图。我想为此表视图启用编辑,以便用户可以删除项目。
我目前有这个代码:
var strings = [String]() {
didSet {
observable.accept(strings)
}
}
let observable = BehaviorRelay(value: [String]())
let disposeBag = DisposeBag()
override func viewDidLoad() {
tableView.dataSource = nil
let dataSource = RxTableViewSectionedAnimatedDataSource<StringSection>(configureCell: {
(dataSource, collectionView, indexPath, string) -> UITableViewCell in
let cell = collectionView.dequeueReusableCell(withIdentifier: "cell", for: indexPath)
cell.textLabel?.text = string
return cell
})
observable.asObservable()
.map {
[StringSection(items: $0)]
}
.bind(to: self.tableView.rx.items(dataSource: dataSource))
.disposed(by: disposeBag)
// X
strings = ["Item 1", "Item 2", "Item 3"]
}
为了使它可编辑,我在标记为 X 的地方添加了这个:
tableView.rx.itemDeleted
.subscribe(onNext: { _ = self.strings.remove(at: $0.row) })
.disposed(by: disposeBag)
navigationItem.rightBarButtonItem = editButtonItem
并且还覆盖了这个方法:
override func tableView(_ tableView: UITableView, editingStyleForRowAt indexPath: IndexPath) -> UITableViewCell.EditingStyle {
return .delete
}
但是,当我按下编辑按钮时,表格视图单元格没有任何变化。我在左侧没有看到红色的“-”按钮。我也无法向左滑动单元格以显示删除按钮。
我还需要做什么才能启用编辑?
解决方案
我认为您项目中的某个地方已设置UITableView
为editing
模式。在代码片段之后,例如,在Github中,允许UITableView
像这样编辑:
extension EditingExampleViewController {
static func dataSource() -> RxTableViewSectionedAnimatedDataSource<NumberSection> {
return RxTableViewSectionedAnimatedDataSource(
animationConfiguration: AnimationConfiguration(insertAnimation: .top,
reloadAnimation: .fade,
deleteAnimation: .left),
configureCell: { (dataSource, table, idxPath, item) in
let cell = table.dequeueReusableCell(withIdentifier: "Cell", for: idxPath)
cell.textLabel?.text = "\(item)"
return cell
},
titleForHeaderInSection: { (ds, section) -> String? in
return ds[section].header
},
canEditRowAtIndexPath: { _, _ in
return true
},
canMoveRowAtIndexPath: { _, _ in
return true
}
)
}
}
我设置UITableView
的editing
模式是false
,我可以向左滑动以删除单元格
推荐阅读
- swift - SwiftUI:应如何制定 ForEach 视图以将字符串从数组转换为 Text() 视图?
- python - 当表已经存在时,Dataframe to_sql 等待很长时间
- swift - macOS NSTextField,NSStackView,NSScrollView 如何在可滚动列表中实现包裹文本?
- php - PHP AJAX Jquery 日历需要重置 beforeShowDay 值吗?
- r - 在多列 R 中查找具有最多匹配的列
- javascript - 等待 Firestore 集合/文档上的循环结束,然后在 Node.js 中发送输出
- angular - mat-tab 标签 flex wrap 响应式
- node.js - nativescript-socketio 未连接到 Node Js 服务器
- go - Windows exec runner 无法克隆 git repo
- javascript - WebGLInput.captureAllKeyboardInput 奇怪的行为。可以启用,但不能禁用