Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

RxSwift Swift - Select/Deselect multiple rows in tableView

I can select rows in tableView.rx but I'm wonder how to get array of final selection. I mean the result array of selecting some rows and then deselecting some rows.

Here's my code:

 Observable.zip(tableView.rx.modelSelected(Country.self), tableView.rx.itemSelected).subscribe(onNext: { (selectedCountry, indexPath) in
            print("2 selected Country is \(selectedCountry) and indexPath is \(indexPath.row)")
            self.selectedCountries.append(selectedCountry)

I want also see selected rows as marked.

I appreciate for your helping. thanks

like image 722
Reza Kashkoul Avatar asked Nov 18 '25 13:11

Reza Kashkoul


1 Answers

First you have to use this:

tableView.allowsMultipleSelection = true

Then you could use these to do what ever you want:

  let selectedItems = tableView.rx.modelSelected(Country.self).subscribe { item in
        print("selected: \(item)")
        print("selected rows: \(self.tableView.indexPathsForSelectedRows)")
  }
  let deselectedItems = tableView.rx.modelDeselected(Country.self).subscribe { item in
        print("deselected: \(item)")
  }
like image 99
Ahmadreza Avatar answered Nov 20 '25 04:11

Ahmadreza



Donate For Us

If you love us? You can donate to us via Paypal or buy me a coffee so we can maintain and grow! Thank you!