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;
}

 

 

demo

转载于:https://www.cnblogs.com/wangchan/p/8808572.html

  • 0
    点赞
  • 1
    收藏
    觉得还不错? 一键收藏
  • 0
    评论

“相关推荐”对你有帮助么?

  • 非常没帮助
  • 没帮助
  • 一般
  • 有帮助
  • 非常有帮助
提交
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值