iOS: 如何使用 RxSwift 删除表视图中的单元格

iOS: How to delete cell in tableview using RxSwift

我刚刚开始使用 RxSwift 并面临一些挑战。我已经创建了具有多个部分的表格视图,并且能够点击并获取详细信息。但是,如果我尝试删除任何特定的单元格,它就不起作用。我不确定我在 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([=11=])
      }
      .disposed(by: disposeBag)

    tableView.rx
      .setDelegate(self)
      .disposed(by: disposeBag)
  }

问题

tableView.rx.itemDeleted 触发包含 indexPath 的事件,其中发生了删除操作。您的数据更改应由您处理。你没有得到任何更新,因为你没有改变任何东西,你只是打印 indexPath

解决方案

由于您使用的是 viewModel.getUsers(),根据您的代码判断,您 returns 是 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: [=14=]) })
    .disposed(by: disposeBag)

现在应该可以删除了。