How can I take control of canEditRowAtIndexPath when using the RxCocoa extension for UITableView

Just for the record, before I start with my question, I use Swift 2 and xcode 7 beta.

So, I use RxSwift and RxCocoa to bind my ViewModel to TableView. In my UITableViewController viewDidLoad, I am doing this binding ...

someCollectionOfViewModels .bindTo(self.tableView.rx_itemsWithCellIdentifier("SomeCell")) { (_, viewModel, cell: SomeTableViewCell) in cell.tripViewModel = viewModel } 

someCollectionOfViewModels is just an array of view models wrapped in a <> variable, so Variable <[SomeViewModel]>. cell.tripViewModel is of type SomeViewModel.

None of this has much to do with the problem, it all works fine, and when my view model is updated, my table is updated. The problem is that I want my table cells not to be edited, and the UITableViewDataSource, which RxCocoa puts in place for the above binding, does not implement the optional canEditRowAtIndexPath method for this protocol, and by default it is true, making the cells editable. I know that I can implement my own RxTableViewDataSourceType, and it's pretty simple, but a lot of code to get this little thing to work. I'm new to both RxSwift and iOS, did I miss something simple? Is there a way in closing above where I have access to a UITableCellView to set some property in the cell itself to make it inaccessible for editing? How about something in the RxCocoa extensions that I am missing. If I need to, I will leave and write my own RxTableViewDataSource, but I thought I would ask first.

Thanks,

-Bill

+6
source share
1 answer

I posted the same question on a Github project and got an answer. The key should be rx_setDelegate(self) and implement the UITableViewDelegate as follows:

 class SimpleTableViewExampleViewController : ViewController, UITableViewDelegate { @IBOutlet weak var tableView: UITableView! override func viewDidLoad() { super.viewDidLoad() let items = Observable.just([ "First Item", "Second Item", "Third Item" ]) items .bindTo(tableView.rx_itemsWithCellIdentifier("Cell", cellType: UITableViewCell.self)) { (row, element, cell) in cell.textLabel?.text = "\(element) @ row \(row)" } .addDisposableTo(disposeBag) tableView .rx_modelSelected(String) .subscribeNext { value in DefaultWireframe.presentAlert("Tapped `\(value)`") } .addDisposableTo(disposeBag) tableView.rx_setDelegate(self) .addDisposableTo(disposeBag) } func tableView(tableView: UITableView, editingStyleForRowAtIndexPath: NSIndexPath) -> UITableViewCellEditingStyle { return UITableViewCellEditingStyle.None } } 

Thanks to Krunoslav Zaher in the RxSwift project for providing an answer.

+4
source

All Articles