Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

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

Just for the record before I launch into my question I am using Swift 2 and xcode 7 beta.

So I am using RxSwift and RxCocoa to bind my ViewModel to the 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 that is really pertinent to the problem, it all works great and when my view model updates, my table updates. The problem is that I want my table cells to be non-editable and the UITableViewDataSource that RxCocoa puts in place for the above binding doesn't implement the optional canEditRowAtIndexPath method on this protocol and it defaults to true making the cells editable. I know I can implement my own RxTableViewDataSourceType and that is fairly straightforward but a lot of code just to get this one little thing to work. I am fairly new to both RxSwift and iOS, am I missing something simple? Is there a way in the closure above where I have access to the UITableCellView to set some property on the cell itself to make it non-editable? How about something in the RxCocoa extensions that I am missing. If I have to I will go off and write my own RxTableViewDataSource, but I thought I would ask here first.

Thanks,

-Bill

like image 548
Bill Dyer Avatar asked Sep 25 '15 03:09

Bill Dyer


1 Answers

I posted this same question over in the Github project and received the answer. The key is to rx_setDelegate(self) and implement UITableViewDelegate like so:

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 the answer.

like image 147
Greg Ferreri Avatar answered Nov 03 '22 05:11

Greg Ferreri