Creating view-model for each UITableViewCell

后端 未结 2 1043
陌清茗
陌清茗 2021-01-29 22:55

I\'m stuck on a design decision with creating view-models for table view\'s cells. Data for each cell is provided by a data source class (has an array of Contacts)

2条回答
  •  太阳男子
    2021-01-29 23:12

    Unless you have a specific problem that's solved with Model-View-ViewModel then attempting to adopt it just for 'best practices' is going to end up introducing a lot of unnecessary complexity.

    Your data-source is what's responsible for populating your table. Nothing other than your datasource needs a reference to contacts as it will update your table with this data.

    View Models only come into play when you need to do complex UI interactions and updates. The VM is responsible for encapsulating the state of your view, things like...

    1. Values of textfields
    2. Which checkboxes/radio buttons are selected
    3. Colors of elements
    4. Animation logic
    5. Dependencies between UI elements

    When changes are made to your View, your View Model is responsible for making updates to your Model (when necessary) in order to reflect the changes that have been made to that Model through the UI.

    With all that said, View Models don't make sense in IOS that's because IOS makes use of View Controllers in the design methodology called MVC (Model-View-Controller)

提交回复
热议问题