objc.io #1 Lighter View Controller

分离tableView的datasource

将datasource单独分离出来,为view controller减轻代码量

  • 方式一:通过block处理cell
  • 方式二:通过delegate处理cell
    #import 
    #import 
    @protocol PhotoDelegate;
    typedef void (^PhotoViewCellConfigureBlock)(id cell,id item) ;

    @interface PhotoDataSource : NSObject 

    @property (nonatomic, weak) id delegate;


    - (instancetype)initWithItem:(id)item
              cellIdentifier:(NSString *)identifier
        configurePhotoCellBlock:(PhotoViewCellConfigureBlock)configureBlock;

    @end


    @protocol PhotoDelegate 

    @required
    - (void)tableViewCell:(UITableViewCell *)cell item:(id)item;
    @end

    #import "PhotoDataSource.h"

    @interface PhotoDataSource ()
    @property (nonatomic, strong) NSArray *items;
    @property (nonatomic, strong) NSString *identifier;
    @property (nonatomic, strong) PhotoViewCellConfigureBlock  photoCellBlock;

    @end

    @implementation PhotoDataSource

    - (instancetype)init
    {
        return  nil;
    }

    - (instancetype)initWithItem:(NSArray *) items
          cellIdentifier:(NSString *)identifier
    configurePhotoCellBlock:(PhotoViewCellConfigureBlock)configureBlock
    {
       self = [super init];

      if (self) {
         self.items = items;
            self.identifier = identifier;
            self.photoCellBlock = configureBlock;
      }

      return self;
    }

    - (id)itemAtIndexPath:(NSIndexPath *) indexPath
    {
       return self.items[indexPath.row];
    }


    #pragma mark - 
    #pragma mark TableViewDataSource

    - (NSInteger)numberOfSectionsInTableView:(nonnull UITableView *)tableView
    {
       return 1;
    }

    - (NSInteger)tableView:(nonnull UITableView *)tableView numberOfRowsInSection:(NSInteger)section
    {
       return self.items.count;
    }

    - (nonnull UITableViewCell *)tableView:(nonnull UITableView *)tableView
             cellForRowAtIndexPath:(nonnull NSIndexPath *)indexPath
    {
        UITableViewCell *cell = [tableView  dequeueReusableCellWithIdentifier:self.identifier forIndexPath:indexPath];

     id item = [self itemAtIndexPath:indexPath];

        //self.photoCellBlock(cell,item);
     [self.delegate tableViewCell:cell item:item];

     return cell;
    }
    @end

    #import "ViewController.h"
    #import "PhotoDataSource.h"

    @interface ViewController ()
    @property (nonatomic, strong) PhotoDataSource * photoDataSource;
    @property (nonatomic, strong) NSArray *items;
    @end

    @implementation ViewController

    - (void)viewDidLoad {
        [super viewDidLoad];
        self.items = @[@"are you ok", @"I am ok", @"what is your        name?", @"Thank you"];

     [self.tableView registerClass:[UITableViewCell class]
       forCellReuseIdentifier:@"PhotoCell"];
     [self setupPhotoDataSource];
    }

    - (void)setupPhotoDataSource
    {
      PhotoViewCellConfigureBlock photoBlock = ^(UITableViewCell * cell, NSString *text)
     {
         cell.textLabel.text = text;
       };

     self.photoDataSource = [[PhotoDataSource alloc] initWithItem:self.items
                                              cellIdentifier:@"PhotoCell"
                                     configurePhotoCellBlock:photoBlock];

        self.photoDataSource.delegate = self;
     self.tableView.dataSource = self.photoDataSource;
     self.tableView.delegate = self;

    }

    #pragma mark - talkingDelegate

    - (void)tableViewCell:(UITableViewCell *)cell item:(id)item
    {
        cell.textLabel.text = item;
       cell.imageView.image = [UIImage imageNamed:@"test.jpg"];
    }
    @end

将业务逻辑移到 Model 中

即将与model有关的业务逻辑通过分类或者直接在model中直接代替实现,减轻view controller的代码量

将网络请求移到 Model 中

将网络请求移动model层,封装在一个类中,后面view controller就可以通过回调来请求网络了。好处在于缓存和错误控制也可以在这个类里面实现

把 View 代码移到 View 层

不应该在view controller中构建复杂的view,而应该单独将view分隔出来使用,更加简洁明了

你可能感兴趣的:(objc.io #1 Lighter View Controller)