After reading Brent Simmons’ response to Ash Furrow’s post on deadly sins in modern Objective-C, one of Brent’s additional sins in his list got me thinking:
Passing model objects to a UITableViewCell subclass.
I was curious about that so I asked him on Twitter and it sparked a lot of passionate back and forth on the topic. Brent followed up with some more explaination:
In the case of table views, there will be an object, typically a UIViewController or UITableViewController, that implements (usually both) UITableViewDataSource and UITableViewDelegate protocols. That object is the controller, and it has the job of coming between the model and the UITableViewCell.
He’s concerned about building a view with too much knowledge about the model
object. And he makes a good point because that’s why the
UITableViewDataSource
concept is there. The data source’s purpose is to pump
the data from the model to the cell.
I get that the “controller” is the mediator of the model state and the view in the flavor of MVC that Apple espouses, but it would seem that putting the knowledge about transforming that raw model data into displayable things would be fine for the cell to do. I was planning to write up a post about this but Paul Goracke beat me to it and did a much better job than I could have:
When a UITableViewCell subclass accepts a model object parameter and updates its constituent subviews as I have described, it is behaving as a data transformer, not a controller. It does not care about any future updates to the model unless the controller tells it to transform the updated model object, or to transform a completely different model instance.
Check out the rest of Paul’s post. It’s full of some interesting reflection on the way Apple uses the MVC paradigm. Brent followed up on Paul’s post to clarify his position a little better, and it’s clear that they both agree on a key point: cells should be stupid simple.
I think that’s wise as well. Table cells are meant to be reused as quickly as possible. Because cells are reused at a moments notice, the cell cannot be the place where transformed data is cached for later reuse. I think that’s part of the reason behind the creation of a “data source” that feeds information to the cell. The data source pulls from the actual model collection but it can also be a source of caching for complicated data transformation before displaying it.
In the low level, low power world of mobile devices, every little bit helps.
✦ PermalinkMy books...