llanfair
llanfair

Reputation: 1885

RxSwift - auto reloading UITableView initialized with Observable

I have main UITableView with some Todos that are populated used an array of Observables:

// on viewDidLoad
self.todosViewModel.todos.asObservable()
    .bind(to: tableView.rx.items(cellIdentifier: "TodoViewCell", cellType: TodoTableViewCell.self)) { (row, todo, cell) in
        cell.status.image = todo.getStatusImage()
        cell.title.text = todo.title.value
        cell.todoDescription.text = todo.description.value
        cell.dueDate.text = String(describing: Utilities.dateFormatter.string(from: todo.dueDate.value))
}.disposed(by: disposeBag)

On another screen I can add/edit the data and then, click "save" button to append a new todo or modify the one being edited. This works great, except that the tableView don't reload automatically, forcing me to call tableView.reloadData() on viewDidAppear, which is triggered when the other screen is dismissed.

So,

Is there a native way for me to reload a table automatically when then todosViewModel variable is updated?

EDIT:

If on the screen of edition of the todo I reassociate the todosViewModel's value property with the same value, it also works:

self.todosViewModel.todos.value = self.todosViewModel.todos.value

That's pretty ugly, but I only know how to reload a tableView using one of those ways.

Upvotes: 1

Views: 1531

Answers (1)

Timofey Solonin
Timofey Solonin

Reputation: 1403

When I append a new todo or reassociate a new value to the base todos it works. When I edit, no.

That's the whole point. For the UITableView to update, your todos have to emit when an item inside has been edited or you have to bind something from your todo within the cell (this way you cannot change hight of the cell but you can push some dynamic information onto the cell).

Another option would be to map some observable from todo to the index of the cell where it's presented and call UITableView.reloadRows to update your edited cell.

I would recommend you to take a look at RxDataSources examples.

Upvotes: 1

Related Questions