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/

最后编辑于
©著作权归作者所有,转载或内容合作请联系作者
平台声明:文章内容(如有图片或视频亦包括在内)由作者上传并发布,文章内容仅代表作者本人观点,简书系信息发布平台,仅提供信息存储服务。

推荐阅读更多精彩内容

  • **2014真题Directions:Read the following text. Choose the be...
    又是夜半惊坐起阅读 13,466评论 0 23
  • 之前有报道苹果CEO库克已经有预料苹果手机销量可能有下滑的趋势,所以iphone可能进行产品线的调整,因此业内人士...
    耿彪阅读 1,898评论 0 1
  • 文/醉过三旬 曾几何时,我们都是一张稚嫩的脸庞,坐在彼此陌生的身旁,谁也不知我们日后竟会成为互相依靠的肩膀。 曾几...
    舟自衡阅读 1,332评论 1 3
  • 怀揣着不一样的梦想,就要付出不一样的努力 有的人努力打游戏 有的人努力出去玩 有的人努力去学习 有的人努力做科研 ...
    梦花生阅读 1,592评论 0 2