Upgrade to Pro — share decks privately, control downloads, hide ads and more …

Lighter View Controllers

Lighter View Controllers

Talk at Cocoaheads.nl

(Architecture image by Manu Cornet)

Chris Eidhof | @chriseidhof

February 18, 2014
Tweet

More Decks by Chris Eidhof | @chriseidhof

Other Decks in Programming

Transcript

  1. Table View Controllers ☆ Editing Mode ☆ Keyboard notifications ☆

    Flashing scroll indicators ☆ Clearing Selection ☆ Pull to Refresh
  2. Creating a separate data source object - (NSInteger)tableView:(UITableView*)tableView numberOfRowsInSection:(NSInteger)sectionIndex {

    id<NSFetchedResultsSectionInfo> section; section = self.frc.sections[sectionIndex]; return section.numberOfObjects; }
  3. Creating a separate data source object Working with the delegate

    - (UITableViewCell*)tableView:(UITableView*)tv cellForRowAtIndexPath:(NSIndexPath*)ip { id object = [self objectAtIndexPath:indexPath]; id cell = [tv dequeueReusableCellWithIdentifier:ruI forIndexPath:ip]; [self.delegate configureCell:cell withObject:object]; return cell; }
  4. Advantages of a separate data source ☆ Lighter view controller

    ☆ Testable ☆ Reusable And you can do this for other protocols, too...
  5. Configuring a cell - (UITableViewCell*)tableView:(UITableView*)tv cellForRowAtIndexPath:(NSIndexPath*)ip { PhotoCell *cell =

    [tableView dequeueReusableCellWithIdentifier:@"PhotoCell"]; Photo *photo = [self itemAtIndexPath:indexPath]; cell.photoTitleLabel.text = photo.name; NSString* date = [self.dateFormatter stringFromDate:photo.creationDate]; cell.photoDateLabel.text = date; }
  6. Configuring a cell Pulling out the reusable part @implementation PhotoCell

    (ConfigureForPhoto) - (void)configureForPhoto:(Photo *)photo { self.photoTitleLabel.text = photo.name; NSString* date = [self.dateFormatter stringFromDate:photo.creationDate]; self.photoDateLabel.text = date; } @end
  7. Configuring a cell The refactored delegate method - (UITableViewCell *)tableView:(UITableView

    *)tableView cellForRowAtIndexPath:(NSIndexPath *)indexPath { PhotoCell *cell = [tableView dequeueReusableCellWithIdentifier:PhotoCellIdentifier]; [cell configureForPhoto:[self itemAtIndexPath:indexPath]]; return cell; }
  8. Intentions View Controllers may only contain code that effects view

    changes in response to data changes. Source: http://bendyworks.com/geekville/articles/ 2014/2/single-responsibility-principle-ios
  9. Intentions A View Controller may not implement any extra *Delegate

    or *DataSource protocols except for observing Model changes.
  10. Intentions A View Controller may only do work in viewDidLoad

    (or awakeFromNib), viewDidAppear, viewDidDisappear, and in response to an observed change in the model layer.