iOS:如何使用 RxSwift 删除 tableview 中的单元格

Kas*_*hif 3 iphone ios swift rx-swift

我刚刚开始使用 RxSwift 并面临一些挑战。我创建了具有多个部分的 tableview,并且能够点击并获取详细信息。但是,如果我尝试删除任何特定单元格,则它不起作用。我不确定我在 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)
  }
Run Code Online (Sandbox Code Playgroud)

E-R*_*die 7

问题

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

解决方案

由于您使用的是 a viewModel.getUsers(),它会Observable<[SectionModel<String, User>]>从您的代码中返回一个判断。您还应该在 上引入一个方法,该方法viewModel将用于删除特定indexPath.

为了实现这一点,您需要将元素存储在BehaviorSubject. 这将保存数据的当前值,并在更新时将新数据发送给订阅它的人。

let sectionListSubject = BehaviorSubject(value: [SectionModel<String, User>]())
Run Code Online (Sandbox Code Playgroud)

当你初始化你的viewModel,你需要用数据填充这个主题,这样做:

sectionListSubject.onNext([
            SectionModel(model: "First section", items: [
                User(),
                User(),
                User()
                ]),
            SectionModel(model: "Second section", items: [
                User(),
                User(),
                User()
                ])
            ])
Run Code Online (Sandbox Code Playgroud)

那么你的getUsers()方法应该是这样的:

func getUsers() -> Observable<[SectionModel<String, User>]> {
    return sectionListSubject.asObservable()
}
Run Code Online (Sandbox Code Playgroud)

你的最后一步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)
}
Run Code Online (Sandbox Code Playgroud)

现在在代码库上,您只需要更改:

tableView.rx.itemDeleted
    .subscribe(onNext: { self.viewModel.removeItem(at: $0) })
    .disposed(by: disposeBag)
Run Code Online (Sandbox Code Playgroud)

删除现在应该可以工作了。