I have a problem when implementing the MVC-pattern on iOS. I have searched the Internet but seems not to find any nice solution to this problem.
Many UITableViewController
implementations seems to be rather big. Most examples I have seen lets the UITableViewController
implement <UITableViewDelegate>
and <UITableViewDataSource>
. These implementations are a big reason why UITableViewController
is getting big. One solution would be to create separate classes that implements <UITableViewDelegate>
and <UITableViewDataSource>
. Of course these classes would have to have a reference to the UITableViewController
. Are there any drawbacks using this solution? In general I think you should delegate the functionality to other "Helper" classes or similar, using the delegate pattern. Are there any well established ways of solving this problem?
I do not want the model to contain too much functionality, nor the view. I believe that the logic should really be in the controller class, since this is one of the cornerstones of the MVC-pattern. But the big question is:
How should you divide the controller of a MVC-implementation into smaller manageable pieces? (Applies to MVC in iOS in this case)
There might be a general pattern for solving this, although I am specifically looking for a solution for iOS. Please give an example of a good pattern for solving this issue. Please provide an argument why your solution is awesome.
Best Answer
I avoid using
UITableViewController
, as it puts lots of responsibilities into a single object. Therefore I separate theUIViewController
subclass from the data source and delegate. The view controller's responsibility is to prepare the table view, create a data source with data, and hook those things together. Changing the way the tableview is represented can be done without changing the view controller, and indeed the same view controller can be used for multiple data sources that all follow this pattern. Similarly, changing the app workflow means changes to the view controller without worrying about what happens to the table.I've tried separating the
UITableViewDataSource
andUITableViewDelegate
protocols into different objects, but that usually ends up being a false split as almost every method on the delegate needs to dig into the datasource (e.g. on selection, the delegate needs to know what object is represented by the selected row). So I end up with a single object that's both the datasource and delegate. This object always provides a method-(id)tableView: (UITableView *)tableView representedObjectAtIndexPath: (NSIndexPath *)indexPath
which both the data source and delegate aspects need to know what they're working on.That's my "level 0" separation of concerns. Level 1 gets engaged if I have to represent objects of different kinds in the same table view. As an example, imagine that you had to write the Contacts app—for a single contact, you might have rows representing phone numbers, other rows representing addresses, others representing email addresses, and so on. I want to avoid this approach:
Two solutions have presented themselves so far. One is to dynamically construct a selector:
In this approach, you don't need to edit the epic
if()
tree to support a new type - just add the method that supports the new class. This is a great approach if this table view is the only one that needs to represent these objects, or needs to present them in a special way. If the same objects will be represented in different tables with different data sources, this approach breaks down as the cell creation methods need sharing across the data sources—you could define a common superclass that provides these methods, or you could do this:Then in your data source class:
This means that any data source that needs to display phone numbers, addresses etc. can just ask whatever object is represented for a table view cell. The data source itself no longer needs to know anything about the object being displayed.
"But wait," I hear a hypothetical interlocutor interject, "doesn't that break MVC? Aren't you putting view details into a model class?"
No, it doesn't break MVC. You can think of the categories in this case as being an implementation of Decorator; so
PhoneNumber
is a model class butPhoneNumber(TableViewRepresentation)
is a view category. The data source (a controller object) mediates between the model and the view, so the MVC architecture still holds.You can see this use of categories as decoration in Apple's frameworks, too.
NSAttributedString
is a model class, holding some text and attributes. AppKit providesNSAttributedString(AppKitAdditions)
and UIKit providesNSAttributedString(NSStringDrawing)
, decorator categories that add drawing behaviour to these model classes.