objc.io #1 Lighter View Controller

分離tableView的datasource

將datasource單獨分離出來,為view controller減輕代碼量

  • 方式一:通過block處理cell
  • 方式二:通過delegate處理cell
    #import <Foundation/Foundation.h>
    #import <UIKit/UIKit.h>
    @protocol PhotoDelegate;
    typedef void (^PhotoViewCellConfigureBlock)(id cell,id item) ;

    @interface PhotoDataSource : NSObject <UITableViewDataSource>

    @property (nonatomic, weak) id<PhotoDelegate> delegate;


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

    @end


    @protocol PhotoDelegate <NSObject>

    @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 ()<UITableViewDataSource,UITableViewDelegate,PhotoDelegate>
    @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分隔出來使用,更加簡潔明了

最后編輯于
?著作權歸作者所有,轉載或內容合作請聯系作者
平臺聲明:文章內容(如有圖片或視頻亦包括在內)由作者上傳并發布,文章內容僅代表作者本人觀點,簡書系信息發布平臺,僅提供信息存儲服務。

推薦閱讀更多精彩內容