Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Update Model in UITableViewCell class #75

Closed
EngrAhsanAli opened this issue May 14, 2020 · 7 comments
Closed

Update Model in UITableViewCell class #75

EngrAhsanAli opened this issue May 14, 2020 · 7 comments
Labels

Comments

@EngrAhsanAli
Copy link

Hi!

In order to save state of the current cell, how to update the model (or any other approach) so to resume the state after scrolling.

E.g: A very simple case, keep UIButton selected state in model class, so after each call of cellForRow, it can be reset to the desired state.

What is equivalent to #this in DTTableViewManager?

@DenTelezhkin
Copy link
Owner

Hi!

You should keep those changes in your data model to be able to update cell state in update(with:) model of ModelTransfer protocol on your cell subclass.

@EngrAhsanAli
Copy link
Author

Hello!

How do I keep changes in my ModelTransfer data model, as in update(with: ) its immutable.

Eg: I am trying to update like the following example.

func update(with model: MyResponse) {
        (model as? MyResponse)?.state = 5 // this is immutable
}

@DenTelezhkin
Copy link
Owner

You can either

a) make model mutable(class-based), and save state there.
b) capture state in view controller instead, for example for selection:

manager.configureCell(CellSubclass.self) { cell, model, indexPath in
    // inject closure to call, when state changes(or subscribe to Rx event, if you use Rx.)
   cell.onSelectedClosure = { 
   // capture indexPath here, for example for array of selected cells.
   }
}

@EngrAhsanAli
Copy link
Author

a) The ModelTransfer has an immutable model. I have a class-based model.

func update(with model: MyResponse) // MyResponse is class

b) the model in the following is also immutable. How do I update my model

manager.configureCell(CellSubclass.self) { cell, model, indexPath in

@DenTelezhkin
Copy link
Owner

If your model is class based, it sure is mutable. So you should be able to set state values in var properties there. Generally though, it's indeed best to keep models immutable and save state in your view controller / view model layer.

@EngrAhsanAli
Copy link
Author

I understand your point. I am actually implementing in MVVM, and not saving the models in ViewModel class. Just to add data to tableView and managing its pagination when I add more items. So for me, I couldn’t update the model I supplied to DTTableViewManager when I added the data.

I think I need to get the state saved elsewhere like ViewModel and upon manager.didEndDisplaying I could reset. I don’t know it would be the best practice but certainly I am seeking for it!

@DenTelezhkin
Copy link
Owner

Whatever you feel is the best for you codebase, it’s hard to advise without knowing context.

As for updating cell state, I’d say configureCell or willDisplayCell are the best places so that state is updated correctly before user sees a cell.

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Labels
Projects
None yet
Development

No branches or pull requests

2 participants