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...
    又是夜半驚坐起閱讀 9,828評論 0 23
  • 之前有報道蘋果CEO庫克已經有預料蘋果手機銷量可能有下滑的趨勢,所以iphone可能進行產品線的調整,因此業內人士...
    耿彪閱讀 263評論 0 1
  • 文/醉過三旬 曾幾何時,我們都是一張稚嫩的臉龐,坐在彼此陌生的身旁,誰也不知我們日后竟會成為互相依靠的肩膀。 曾幾...
    舟自衡閱讀 188評論 1 3
  • 懷揣著不一樣的夢想,就要付出不一樣的努力 有的人努力打游戲 有的人努力出去玩 有的人努力去學習 有的人努力做科研 ...
    夢花生閱讀 237評論 0 2