Rxswift: Swipe to delete

Created on 29 May 2017  路  4Comments  路  Source: ReactiveX/RxSwift

Hi, all.

Plz help to understand where the error is, I can't implement simple swipe for delete on UITableView
`
// Cells with section
dataSource.configureCell = { (_, table: UITableView, indexPath: IndexPath, item: CountryCode) in
let cell = table.dequeueReusableCell(withType: CountryCodeCell.self, forRowAt: indexPath)
cell.configure(with: item)
return cell
}

// Cell selected
tableView.rx
  .modelSelected(CountryCode.self)
  .subscribe(viewModel.selectedCountry)
  .disposed(by: disposeBag)

// Section header
dataSource.titleForHeaderInSection = { dataSource, sectionIndex in
  guard self.search.text!.isEmpty else {
    if self.viewModel.countries.value?.first?.items.count == 0 {
      return "No Found Result for \(self.search.text!)"
    }
    return nil
  }
  return dataSource[sectionIndex].header
}

// Section index header
dataSource.sectionIndexTitles = { _ in
  guard self.search.text!.isEmpty else {
    return nil
  }
  var sectionTiles: [String]? = [String]()
  for letter in self.viewModel.letters {
    sectionTiles?.append(String(letter))
  }
  return sectionTiles
}

// Section index tap
dataSource.sectionForSectionIndexTitle = { _, _, number in
  return number + 1
}

dataSource.canEditRowAtIndexPath = { _ in
  return true
}

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


func tableView(_ tableView: UITableView, editingStyleForRowAt indexPath: IndexPath) -> UITableViewCellEditingStyle {
return .delete
}

func tableView(tableView: UITableView, commitEditingStyle editingStyle: UITableViewCellEditingStyle, forRowAtIndexPath indexPath: NSIndexPath) {
//if editingStyle == .delete {
// Alert
//}
print("DELETE")
}
`

But my cells have no action (there is no DELETE button) on swipe action.

Most helpful comment

But is not an editing, I have the same question.
If i want remove an item with swipe action, not in edit mode. How can I use rxdatasource?

All 4 comments

Hi @mrengine ,

there is example called TableView with editing in RxExample app inside this repo that has working swipe to delete, so you can check out that code.

It doesn't look like this is an issue, so closing this one.

But is not an editing, I have the same question.
If i want remove an item with swipe action, not in edit mode. How can I use rxdatasource?

But is not an editing, I have the same question.
If i want remove an item with swipe action, not in edit mode. How can I use rxdatasource?

I found it just now.

First disable tableView editing mode

tableView.setEditing(false, animated: true)

Second Add canEditRow when init data source

let ds: yourDataSource = yourDataSource(configureCell: {(dataSource, tv, indexPath, element) in
            let cell: YourCell = tv.dequeueReusableCell(withIdentifier: "YourCell", for: indexPath) as! YourCell
           return cell
        }, titleForHeaderInSection: { _,_  -> String? in
            return ""
        }, titleForFooterInSection: { _,_  -> String? in
            return ""
        }, canEditRowAtIndexPath: { _,_  -> Bool in
            return true
        })

Just subscirbe to ItemDeleted and dont set delegate

tableView.rx.itemDeleted
            .subscribe(onNext: {  [weak self] indexPath in
                guard let self = self else { return }
                //Do delete here
            }).disposed(by: disposeBag)

Can we have this https://medium.com/@mrachamallu/multiple-row-selection-with-check-buttons-for-uitableviewcells-programmatically-efb36507891 in RxSwift? How do I implement multiple selection of uitableview in rx ?

Was this page helpful?
0 / 5 - 0 ratings

Related issues

retsohuang picture retsohuang  路  3Comments

Z-JaDe picture Z-JaDe  路  3Comments

angerman picture angerman  路  3Comments

kzaher picture kzaher  路  3Comments

jaumard picture jaumard  路  3Comments