UITableViewCell + Model ObjectBryn BodayleiOS Developer
Three Ways?
1. View controller configuring2. Pass model to cell3. Categories
1. View controller configuring- “Default” method- Maintains strict MVC- Bad reuse: copy & paste- Easy but cluttered- Dynam...
1. View controller configuring- (UITableViewCell *)tableView:(UITableView *)tableView cellForRowAtIndexPath:(NSIndexPath *...
1. View controller configuring  - (UITableViewCell *)tableView:(UITableView *)tableView cellForRowAtIndexPath:(NSIndexPath...
1. View controller configuring-(CGFloat)tableView:(UITableView *)tableView heightForRowAtIndexPath:(NSIndexPath *)indexPat...
2. Pass model to cell- Quick & easy- Easy dynamic heights- Tightly couples cell to model object- Clean interface- Nimbus F...
When a UITableViewCell subclass accepts a model object parameter andupdates its constituent subviews as I have described, ...
2. Pass model to cell    - (UITableViewCell *)tableView:(UITableView *)tableView cellForRowAtIndexPath:(NSIndexPath *)inde...
2. Pass model to cell    -(CGFloat)tableView:(UITableView *)tableView heightForRowAtIndexPath:(NSIndexPath *)indexPath    ...
3. Categories- Middle ground- Loose coupling- Extra files- Subviews must be public- Multiple model objects for one cell
3. Categories #import "FAScoreboardCell.h"@class Athlete;@interface FAScoreboardCell (Athlete)+ (NSInteger)heightForText:(...
Further Reading-Brent Simmons: “UITableViewCell Is Not a Controller”- http://inessential.com/2012/12/31/uitableviewcell_is...
Questions?
Upcoming SlideShare
Loading in …5
×

UITableViewCell + Model Object

3,242 views

Published on

Super brief talk I gave about the various approaches for configuring a UITableViewCell with a model object. I gave this talk at the March Cocoaheads meeting in 2013. More information about the meeting below.

http://www.brynbodayle.com/nashville-cocoaheads-recap-321/

Published in: Technology
0 Comments
2 Likes
Statistics
Notes
  • Be the first to comment

No Downloads
Views
Total views
3,242
On SlideShare
0
From Embeds
0
Number of Embeds
213
Actions
Shares
0
Downloads
18
Comments
0
Likes
2
Embeds 0
No embeds

No notes for slide

UITableViewCell + Model Object

  1. 1. UITableViewCell + Model ObjectBryn BodayleiOS Developer
  2. 2. Three Ways?
  3. 3. 1. View controller configuring2. Pass model to cell3. Categories
  4. 4. 1. View controller configuring- “Default” method- Maintains strict MVC- Bad reuse: copy & paste- Easy but cluttered- Dynamic cell heights clunky- Goodbye skinny view controllers- Lots of constants
  5. 5. 1. View controller configuring- (UITableViewCell *)tableView:(UITableView *)tableView cellForRowAtIndexPath:(NSIndexPath *)indexPath{ NSString *reuseIdentifier = @"Tweet Cell"; UITableViewCell *cell = [tableView dequeueReusableCellWithIdentifier:reuseIdentifier]; if (nil == cell) { cell = [[UITableViewCell alloc] initWithStyle:UITableViewCellStyleDefaultreuseIdentifier:reuseIdentifier]; cell.textLabel.font = [UIFont systemFontOfSize:14]; cell.textLabel.numberOfLines = 0; cell.textLabel.backgroundColor = [UIColor clearColor]; cell.contentView.backgroundColor = [UIColor colorWithPatternImage:[UIImage imageNamed:@"listbg.png"]]; } cell.textLabel.text = [[_statuses objectAtIndex:indexPath.row] text]; return cell;}
  6. 6. 1. View controller configuring - (UITableViewCell *)tableView:(UITableView *)tableView cellForRowAtIndexPath:(NSIndexPath *)indexPath{! static NSString *CellIdentifier = @"CustomTableViewCell";! CustomTableViewCell *cell = [tableView dequeueReusableCellWithIdentifier:CellIdentifier];! if (cell == nil)! ! cell = [[CustomTableViewCell alloc] initWithStyle:UITableViewCellStyleDefault reuseIdentifier:CellIdentifier]; ModelObject *object = self.data[indexPath.section][indexPath.row]; NSMutableAttributedString *attributedTitle = [[NSMutableAttributedString alloc] initWithString:object.title]; [attributedTitle setAttributes:@{UITextAttributeFont : [UIFont fontWithName:@"Comic Sans" size:20]} range:NSMakeRange(0, 10)]; [attributedTitle setAttributes:@{UITextAttributeFont : [UIFont fontWithName:@"Zapfino" size:20]} range:NSMakeRange(10, 20)]; [cell.titleText setAttributedText:attributedTitle]; [cell.titleText sizeToFit]; cell.detailText.text = object.detail; [cell.titleText sizeToFit]; cell.profileImage.image = object.image; [UIView animateWithDuration:0.5 delay:0 options:UIViewAnimationOptionCurveEaseInOut animations:^(void) { cell.profileImage.alpha = 1; } completion:^(BOOL finished) { }];! return cell;}
  7. 7. 1. View controller configuring-(CGFloat)tableView:(UITableView *)tableView heightForRowAtIndexPath:(NSIndexPath *)indexPath { ModelObject *object = self.data[indexPath.section][indexPath.row]; if(object.image) { CGSize size = [object.detail sizeWithFont:DETAIL_FONT constrainedToSize:CGSizeMake(DETAIL_WIDTH, MAXFLOAT)lineBreakMode:DETAIL_LINE_BREAK_MODE]; return MAX(size.height + CELL_PADDING, 70); } else { CGSize size = [object.detail sizeWithFont:DETAIL_FONT_SMALL constrainedToSize:CGSizeMake(DETAIL_WIDTH_IMAGE,MAXFLOAT) lineBreakMode:DETAIL_LINE_BREAK_MODE]; return MAX(size.height + CELL_PADDING, 50); }}
  8. 8. 2. Pass model to cell- Quick & easy- Easy dynamic heights- Tightly couples cell to model object- Clean interface- Nimbus Framework- Deadly sin?
  9. 9. When a UITableViewCell subclass accepts a model object parameter andupdates its constituent subviews as I have described, it is behaving as a datatransformer, not a controller. It does not care about any future updates to themodel unless the controller tells it to transform the updated model object, orto transform a completely different model instance. - Paul Goracke
  10. 10. 2. Pass model to cell - (UITableViewCell *)tableView:(UITableView *)tableView cellForRowAtIndexPath:(NSIndexPath *)indexPath{! static NSString *CellIdentifier = @"CustomTableViewCell";! CustomTableViewCell *cell = [tableView dequeueReusableCellWithIdentifier:CellIdentifier];! if (cell == nil)! ! cell = [[CustomTableViewCell alloc] initWithStyle:UITableViewCellStyleDefaultreuseIdentifier:CellIdentifier];! ModelObject *object = self.data[indexPath.section][indexPath.row]; [cell updateCellWithObject:object];! return cell;}
  11. 11. 2. Pass model to cell -(CGFloat)tableView:(UITableView *)tableView heightForRowAtIndexPath:(NSIndexPath *)indexPath { ModelObject *object = self.data[indexPath.section][indexPath.row]; return [CustomTableViewCell cellHeightForDetailText:object.detail];-(CGFloat)tableView:(UITableView *)tableView heightForRowAtIndexPath:(NSIndexPath *)indexPath { ModelObject *object = self.data[indexPath.section][indexPath.row]; return [CustomTableViewCell cellHeightForObject:object];}
  12. 12. 3. Categories- Middle ground- Loose coupling- Extra files- Subviews must be public- Multiple model objects for one cell
  13. 13. 3. Categories #import "FAScoreboardCell.h"@class Athlete;@interface FAScoreboardCell (Athlete)+ (NSInteger)heightForText:(NSString *)text;- (void)updateWithAthlete:(Athlete *)athlete;@end
  14. 14. Further Reading-Brent Simmons: “UITableViewCell Is Not a Controller”- http://inessential.com/2012/12/31/uitableviewcell_is_not_a_controller-Paul Goracke: “UITableViewCell Is Not a Controller, But...”- http://corporationunknown.com/blog/2013/01/01/uitableviewcell-is-not-a- controller-but/-Sebastian Rehnby: “Skinnier Controllers Using View Categories”- http://www.sebastianrehnby.com/blog/2013/01/01/skinnier-controllers- using-view-categories/
  15. 15. Questions?

×