UITableViewController Anti-Patterns

Loading...

Flash Player 9 (or above) is needed to view presentations.
We have detected that you do not have it on your computer. To install it, go here.

0 comments

Post a comment

    Post a comment
    Embed Video
    Edit your comment Cancel

    Favorites, Groups & Events

    UITableViewController Anti-Patterns - Presentation Transcript

    1. UITableViewController Anti-Patterns Josh Bassett @nullobject
    2. A Few Thoughts
    3. A Few Thoughts • Learning Cocoa Touch is easy.
    4. A Few Thoughts • Learning Cocoa Touch is easy. • Learning bad habits is easy.
    5. A Few Thoughts • Learning Cocoa Touch is easy. • Learning bad habits is easy. • There’s a lot of sample code out there which teaches you bad habits.
    6. A Few Thoughts • Learning Cocoa Touch is easy. • Learning bad habits is easy. • There’s a lot of sample code out there which teaches you bad habits. • Some Apple samples apps are even guilty of this.
    7. Talking About Design
    8. Talking About Design • Design pattern: a reusable solution to a common problem.
    9. Talking About Design • Design pattern: a reusable solution to a common problem. • Anti-pattern: an ineffective design pattern which is often counterproductive.
    10. Talking About Design • Design pattern: a reusable solution to a common problem. • Anti-pattern: an ineffective design pattern which is often counterproductive. • Code smell: a symptom in the code which indicates a deeper problem.
    11. The Fat Controller
    12. The Fat Controller • A controller polluted with code which belongs in the model or view.
    13. The Fat Controller • A controller polluted with code which belongs in the model or view. • Hard to understand.
    14. The Fat Controller • A controller polluted with code which belongs in the model or view. • Hard to understand. • Very difficult to maintain.
    15. The Fat Controller - (UITableViewCell *)tableView:(UITableView *)tableView cellForRowAtIndexPath:(NSIndexPath *)indexPath { static NSString *CellIdentifier = @"Cell"; UITableViewCell *cell = [tableView dequeueReusableCellWithIdentifier:CellIdentifier]; Animal *animal = [animals objectAtIndex:indexPath.row]; if (cell == nil) { cell = [[[UITableViewCell alloc] initWithStyle:UITableViewCellStyleDefault reuseIdentifier:CellIdentifier] autorelease]; // This is view code. UILabel *nameLabel = [[UILabel alloc] init]; nameLabel.frame = CGRectMake(10, 5, 100, 30); nameLabel.tag = NAME_LABEL_TAG; [cell addSubview:nameLabel]; [nameLabel release]; } ... }
    16. The Fat Controller - (UITableViewCell *)tableView:(UITableView *)tableView cellForRowAtIndexPath:(NSIndexPath *)indexPath { ... UILabel *nameLabel = [cell viewWithTag:NAME_LABEL_TAG]; // This is model code. NSString *name = [animal.name stringByTrimmingCharactersInSet: [NSCharacterSet whitespaceCharacterSet]]; nameLabel.text = name; ... return cell; }
    17. MVC
    18. Tight Coupling View Model Controller One big fat controller
    19. Loose Coupling View Model Controller
    20. Animal Model
    21. Animal @interface Animal : NSManagedObject { NSString *name; } @property (readwrite, assign) NSString *name; @end
    22. Animal @implementation Animal @synthesize name; - (void)setName:(NSString *)value { name = [value stringByTrimmingCharactersInSet:[NSCharacterSet whitespaceCharacterSet]]; } @end
    23. AnimalTableViewCell View
    24. AnimalTableViewCell @interface AnimalTableViewCell : UITableViewCell { Animal *animal; UIButton *speakButton; UILabel *nameLabel; } @property (readwrite, assign) Animal *animal; @end
    25. AnimalTableViewCell - (id)initWithStyle:(UITableViewCellStyle)style reuseIdentifier:(NSString *)reuseIdentifier { if (self = [super initWithStyle:style reuseIdentifier:reuseIdentifier]) { speakButton = [UIButton buttonWithType:UIButtonTypeRoundedRect]; speakButton.frame = CGRectMake(200, 5, 100, 30); [speakButton setTitle:@"Speak" forState:UIControlStateNormal]; [speakButton setTitleColor:[UIColor blackColor] forState:UIControlStateNormal]; [speakButton addTarget:self action:@selector(speakPressed) forControlEvents:UIControlEventTouchUpInside]; [self.contentView addSubview:speakButton]; nameLabel = [[UILabel alloc] init]; nameLabel.frame = CGRectMake(10, 5, 100, 30); [self.contentView addSubview:nameLabel]; } return self; }
    26. AnimalTableViewCell - (void)setAnimal:(Animal *)value { animal = value; nameLabel.text = animal.name; }
    27. AnimalTableViewController Controller
    28. AnimalsTableViewController - (UITableViewCell *)tableView:(UITableView*)tableView cellForRowAtIndexPath:(NSIndexPath *)indexPath { static NSString *CellIdentifier = @"Cell"; AnimalTableViewCell *cell = (AnimalTableViewCell *)[tableView dequeueReusableCellWithIdentifier:CellIdentifier]; if (cell == nil) { cell = [[[AnimalTableViewCell alloc] initWithFrame:CGRectZero reuseIdentifier:CellIdentifier] autorelease]; } cell.animal = [animals objectAtIndex:indexPath.row]; return cell; }
    29. Push To Speak UIButton *speakButton = [UIButton buttonWithType:UIButtonTypeRoundedRect]; speakButton.frame = CGRectMake(200, 5, 100, 30); [speakButton setTitle:@"Speak" forState:UIControlStateNormal]; [speakButton setTitleColor:[UIColor blackColor] forState:UIControlStateNormal]; // Where is this handled? [speakButton addTarget:self action:@selector(speakHandler) forControlEvents:UIControlEventTouchUpInside]; [self.contentView addSubview:speakButton];
    30. AnimalsTableViewController - (void)speakHandler { NSIndexPath *indexPath = [self.tableView indexPathForCell: (UITableViewCell*)[[sender superview] superview]]; AnimalTableViewCell *cell = (AnimalTableViewCell*)[tableView cellForRowAtIndexPath:indexPath]; Animal *animal = cell.animal; // Show alert “Hi I’m a monkey”. }
    31. Delegate
    32. AnimalsTableViewDelegate @protocol AnimalsTableViewDelegate <UITableViewDelegate> - (void)tableView:(UITableView *)tableView didSpeakAtIndexPath: (NSIndexPath *)indexPath; @end
    33. AnimalsTableViewController @interface AnimalsTableViewController : UITableViewController <UITableViewDataSource, AnimalsTableViewDelegate> { NSArray *animals; } @end
    34. AnimalTableViewCell - (void)speakPressed { UITableView *tableView = self.target; NSIndexPath *indexPath = [tableView indexPathForCell:self]; [(id<AnimalsTableViewDelegate>)tableView.delegate tableView:tableView didSpeakAtIndexPath:indexPath]; }
    35. AnimalsTableViewController - (void)tableView:(UITableView *)tableView didSpeakAtIndexPath: (NSIndexPath *)indexPath { AnimalTableViewCell *cell = (AnimalTableViewCell *)[tableView cellForRowAtIndexPath:indexPath]; Animal *animal = cell.animal; // Show alert “Hi I’m a monkey”. }
    36. Questions
    SlideShare Zeitgeist 2009

    + Josh BassettJosh Bassett Nominate

    custom

    1335 views, 0 favs, 1 embeds more stats

    Melbourne CocoaHeads 09/07/2009

    More info about this document

    CC Attribution-ShareAlike LicenseCC Attribution-ShareAlike License

    Go to text version

    • Total Views 1335
      • 1333 on SlideShare
      • 2 from embeds
    • Comments 0
    • Favorites 0
    • Downloads 9
    Most viewed embeds
    • 2 views on http://www.stile.ch

    more

    All embeds
    • 2 views on http://www.stile.ch

    less

    Flagged as inappropriate Flag as inappropriate
    Flag as inappropriate

    Select your reason for flagging this presentation as inappropriate. If needed, use the feedback form to let us know more details.

    Cancel
    File a copyright complaint
    Having problems? Go to our helpdesk?

    Categories