Lighter View Controllers

Separate Out Data Source and Other Protocols

Take the UITableViewDataSource part of your code, and move it to its own class.

# pragma mark Pragma 
- (Photo*)photoAtIndexPath:(NSIndexPath*)indexPath {     
    return photos[(NSUInteger)indexPath.row];
}
- (NSInteger)tableView:(UITableView*)tableView numberOfRowsInSection:(NSInteger)section { 
    return photos.count;
}
- (UITableViewCell*)tableView:(UITableView*)tableView cellForRowAtIndexPath:(NSIndexPath*)indexPath { 
    PhotoCell* cell = [tableView dequeueReusableCellWithIdentifier:PhotoCellIdentifier forIndexPath:indexPath]; 
    Photo* photo = [self photoAtIndexPath:indexPath]; cell.label.text = photo.name; return cell;
}

Let's try to move the array-related code into its own class. We use a block for configuring the cell, but it might as well be a delegate, depending on your use-case and taste.

@implementation ArrayDataSource
- (id)itemAtIndexPath:(NSIndexPath*)indexPath { 
    return items[(NSUInteger)indexPath.row];
}
- (NSInteger)tableView:(UITableView*)tableView numberOfRowsInSection:(NSInteger)section { 
    return items.count;
}
- (UITableViewCell*)tableView:(UITableView*)tableView cellForRowAtIndexPath:(NSIndexPath*)indexPath { 
    id cell = [tableView dequeueReusableCellWithIdentifier:cellIdentifier forIndexPath:indexPath]; 
    id item = [self itemAtIndexPath:indexPath]; 
    configureCellBlock(cell,item); 
    return cell;
}
@end


void (^configureCell)(PhotoCell*, Photo*) = ^(PhotoCell* cell, Photo* photo) { 
    cell.label.text = photo.name;
};

photosArrayDataSource = [[ArrayDataSource alloc] initWithItems:photos cellIdentifier:PhotoCellIdentifier configureCellBlock:configureCell];
self.tableView.dataSource = photosArrayDataSource;

Move Domain Logic into the Model

- (void)loadPriorities { 
    NSDate* now = [NSDate date]; 
    NSString* formatString = @"startDate <= %@ AND endDate >= %@"; 
    NSPredicate* predicate = [NSPredicate predicateWithFormat:formatString, now, now]; 
    NSSet* priorities = [self.user.priorities filteredSetUsingPredicate:predicate]; 
    self.priorities = [priorities allObjects];
}

It is much cleaner to move this code to a category on the User class. Then it looks like this in View Controller.m

- (void)loadPriorities { 
    self.priorities = [self.user currentPriorities];
}

and in User+Extensions.m

- (NSArray*)currentPriorities { 
    NSDate* now = [NSDate date]; 
    NSString* formatString = @"startDate <= %@ AND endDate >= %@"; 
    NSPredicate* predicate = [NSPredicate predicateWithFormat:formatString, now, now]; 
    return [[self.priorities filteredSetUsingPredicate:predicate] allObjects];
}

References:

https://www.objc.io/issues/1-view-controllers/lighter-view-controllers/

你可能感兴趣的:(Lighter View Controllers)