Home > Software design >  How does reusable cells work with ViewModels
How does reusable cells work with ViewModels

Time:01-02

Firstly, I could not find any answer on SO relates to this question, and I'm also not sure how my code works or whether do I violate the reusable cell rule.

Scenario: I have a simple tableView with 30 cells. Each cell has its own ViewModel class. The ViewModels are stored in an array with sorted order based on the ViewModel's property; var number: Int which means the tableView must always display correct sorted data.

Questions:

  1. if I pass a ViewModel to each reusable cell, how can each reusable cell remember which ViewModel to use? Because each cell does not stay at the same position right? (The displayed result of my tableView was always correct)

  2. Did the tableView created pure 30 cells without reusing any cell since I passed a reference type ViewModel to each reusable cell?

Code:

func tableView(_ tableView: UITableView, numberOfRowsInSection section: Int) -> Int {
    Constant.dummyViewModels.count
}

func tableView(_ tableView: UITableView, cellForRowAt indexPath: IndexPath) -> UITableViewCell {
    let cell = tableView.dequeueReusableCell(withIdentifier: "TestTableViewCell", for: indexPath) as! TestTableViewCell

    cell.config(Constant.dummyViewModels[indexPath.row])

    return cell
}

CodePudding user response:

Let's say the height of your table view allows for 8 cells to be displayed at any given time. As you scroll, a cell disappear off the top of the screen and a cell appears at the bottom.

So the table view must create at least 8 cells, one for each currently visible cell. As you scroll and a new row appears at the bottom, for example, the table view will reuse a cell that recently scrolled out of view at the top. The table view then calls cellForRowAt with an index path representing the row at the bottom. You then configure that cell with the appropriate data.

While a cell is on the screen it has already been configured via cellForRowAt and the table view simply moves that view up or down as you scroll. Once it goes off the screen it can be reused for another row as needed.

With that, your questions can be answered:

  1. Each visible cell (all 8 in this example) has each been configured for a specific row while visible. The cell "remembers" which view because it simply displays whichever view model is was configured with last.
  2. The table view reused cells. It did not create 30 cells for 30 rows. It only created 8 cells, for example, since that is how many it needs for the visible number of rows. It doesn't care that you passed a view model to each visible cell. Each reusable cell will be configured with whatever appropriate view model you give it in cellForRowAt.

In short, your use of table view cells is just fine, at least in regard to the reuse you have shown in your question.

  • Related