分離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ò)請(qǐng)求移到 Model 中
將網(wǎng)絡(luò)請(qǐng)求移動(dòng)model層学少,封裝在一個(gè)類中,后面view controller就可以通過回調(diào)來請(qǐng)求網(wǎng)絡(luò)了秧骑。好處在于緩存和錯(cuò)誤控制也可以在這個(gè)類里面實(shí)現(xiàn)
把 View 代碼移到 View 層
不應(yīng)該在view controller中構(gòu)建復(fù)雜的view,而應(yīng)該單獨(dú)將view分隔出來使用,更加簡潔明了