Comment on "Clean table view code" from objc.io #1
Finally I get to read all the great articles of objc.io. Recently I read [Clean table view code](http://www.objc.io/issue-1/table-views.html) (because I want to improve my `UITableView` code) and I have to comment on it, because for me the approach doesn't look that clean.
In **Bridging the Gap Between Model Objects and Cells** [@floriankugler](https://twitter.com/floriankugler) recommends to put the code, populating the `UITableViewCell` subclass into a category of the cell.
[code language="objc"]
@implementation PhotoCell (ConfigureForPhoto)
- (void)configureForPhoto:(Photo *)photo
{
self.photoTitleLabel.text = photo.name;
NSString* date = [self.dateFormatter stringFromDate:photo.creationDate];
self.photoDateLabel.text = date;
}
@end
[/code]
I think this is not a good idea because it puts information about the model into the cell, which belongs to the view layer. Here an example where the problem about this becomes more obvious:
Let's say we are going to build a client for App.net. Posts in the client are represented in the model layer by `DDHPost` objects. If we use the suggested category approach this would result in something like this:
[code language="objc"]
#import "DDHPost.h"
@implementation DDHPostCell (ConfigureForPost)
- (void)configureForPost:(DDHPost*)post {
self.nameLabel.text = post.userName;
self.postLabel.attributedText = [self attributedTextForPost:post];
self.postDateLabel.text = post.dateString;
[self loadAvatarFromURL:post.avatarURL];
}
@end
[/code]
Later in the development we realize that it would be better to generate the attributed text for the post after we have downloaded the posts from the API. Therefore we add an `attributedText` property in the model layer. Now we have to change the `DDHPostCell` category (which belongs to the view layer) to reflect this change even though the cell looks exactly the same after this change. **(The advantage in this case is, that we don't have to change the data source of the table view.)**
I think this conflicts with the Model-View-Controller pattern. I prefer to have a smart table view data source which knows how to deal with the data an the presentation. In fact it already needs to know about the cell and the data object.
Did I miss something? Please let me know what you think! Get in touch with me at [App.net](https://alpha.app.net/dasdom) or [Twitter](https://twitter.com/dasdom).