objc.io #1 Lighter View Controller

分離tableView的datasource

將datasource單獨(dú)分離出來,為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

將業(yè)務(wù)邏輯移到 Model 中

即將與model有關(guān)的業(yè)務(wù)邏輯通過分類或者直接在model中直接代替實(shí)現(xiàn),減輕view controller的代碼量

將網(wǎng)絡(luò)請求移到 Model 中

將網(wǎng)絡(luò)請求移動model層,封裝在一個類中,后面view controller就可以通過回調(diào)來請求網(wǎng)絡(luò)了。好處在于緩存和錯誤控制也可以在這個類里面實(shí)現(xiàn)

把 View 代碼移到 View 層

不應(yīng)該在view controller中構(gòu)建復(fù)雜的view,而應(yīng)該單獨(dú)將view分隔出來使用,更加簡潔明了

最后編輯于
?著作權(quán)歸作者所有,轉(zhuǎn)載或內(nèi)容合作請聯(lián)系作者
【社區(qū)內(nèi)容提示】社區(qū)部分內(nèi)容疑似由AI輔助生成,瀏覽時請結(jié)合常識與多方信息審慎甄別。
平臺聲明:文章內(nèi)容(如有圖片或視頻亦包括在內(nèi))由作者上傳并發(fā)布,文章內(nèi)容僅代表作者本人觀點(diǎn),簡書系信息發(fā)布平臺,僅提供信息存儲服務(wù)。

相關(guān)閱讀更多精彩內(nèi)容

友情鏈接更多精彩內(nèi)容