iOS【专题】UITableView篇--datasource与delegate的分离
在项目中tableview和collectionview用的很频繁,然而每次用到都要写一大堆代理方法,非常繁琐,因此衍生出把代理方法抽离出来的想法:
代码如下:
// // AMYDataSource.h // TableViewDataSource // // Created by amy on 2018/4/12. // Copyright © 2018年 amy. All rights reserved. // #import <Foundation/Foundation.h> #import <UIKit/UIKit.h> typedef void (^CellConfigureBefore)(id cell, id model,NSIndexPath *indexPath); @interface AMYDataSource : NSObject <UITableViewDataSource> @property (nonatomic ,strong) NSMutableArray *dataArray; //自定义 -(id)initWithIdentifier:(NSString *)identifier configureBlock:(CellConfigureBefore)before; //sb @property (nonatomic ,strong)IBInspectable NSString *cellIdentifier; @property (nonatomic, copy) CellConfigureBefore cellConfigureBefore; -(void)addDataArray:(NSArray *)datas; -(id)modelsAtIndexPath:(NSIndexPath *)indexPath; @end
// AMYDataSource.m // TableViewDataSource // // Created by amy on 2018/4/12. // Copyright © 2018年 amy. All rights reserved. // #import "AMYDataSource.h" @implementation AMYDataSource -(id)initWithIdentifier:(NSString *)identifier configureBlock:(CellConfigureBefore)before { if (self=[super init]) { _cellIdentifier = identifier; _cellConfigureBefore = [before copy]; } return self; } -(NSMutableArray*)dataArray { if (!_dataArray) { _dataArray=@[].mutableCopy; } return _dataArray; } -(void)addDataArray:(NSArray *)datas{ if (!datas) return; if (datas.count>0) { [self.dataArray removeAllObjects]; } [self.dataArray addObjectsFromArray:datas]; } -(id)modelsAtIndexPath:(NSIndexPath *)indexPath{ return self.dataArray.count >indexPath.row ? self.dataArray[indexPath.row] : nil; } #pragma mark UITableViewDataSource -(NSInteger)tableView:(UITableView *)tableView numberOfRowsInSection:(NSInteger)section{ return !self.dataArray ? 0 : self.dataArray.count; } -(UITableViewCell *)tableView:(UITableView *)tableView cellForRowAtIndexPath:(NSIndexPath *)indexPath{ UITableViewCell *cell = [tableView dequeueReusableCellWithIdentifier:self.cellIdentifier]; # warning 注意此处的cell如果没在tableview中注册,会崩溃 id model = [self modelsAtIndexPath:indexPath]; if (self.cellConfigureBefore) { self.cellConfigureBefore(cell, model, indexPath); } return cell; } @end
使用:
// ViewController.m // TableViewDataSource // // Created by amy on 2018/4/12. // Copyright © 2018年 amy. All rights reserved. // #import "ViewController.h" #import "AMYDataSource.h" @interface ViewController ()<UITableViewDelegate> @property (weak, nonatomic) IBOutlet UITableView *tableView; @property (nonatomic ,strong)AMYDataSource *dataSource; @end @implementation ViewController static NSString *cellIden = @"cell"; - (void)viewDidLoad { [super viewDidLoad]; // Do any additional setup after loading the view, typically from a nib. self.dataSource=[[AMYDataSource alloc]initWithIdentifier:cellIden configureBlock:^(id cell, id model, NSIndexPath *indexPath) { UITableViewCell *cell1=cell; cell1.backgroundColor=[UIColor colorWithRed:(arc4random()%255+1)/255.0 green:(arc4random()%255+1)/255.0 blue:(arc4random()%255+1)/255.0 alpha:1]; cell1.textLabel.text=model; }]; [self.dataSource addDataArray:@[@"池塘边的一只猫",@"池塘边的一只🐱",@"池塘边的一只🎩"]]; self.tableView.dataSource=self.dataSource; self.tableView.delegate=self; [self.tableView registerClass:[UITableViewCell class] forCellReuseIdentifier:cellIden]; } -(CGFloat)tableView:(UITableView *)tableView heightForRowAtIndexPath:(NSIndexPath *)indexPath { return 100.0f; }