一个公共的TableView,然后不会为每个TableView加delegate和datasource

.h
//
// PublicTableView.h
// JointCrm
//
// Created by Mac on 15/10/27.
// Copyright © 2015年 Mac. All rights reserved.
//

#import <UIKit/UIKit.h>

typedef void(^TableBlock)(UITableView *tableView,NSIndexPath *indexPath);

@interface PublicTableView : UITableView

/**
* 定义一个公共的TableView,然后可以在使用tableview的地方使用
*
* @param frame UITableView的frame
* @param style UITableViewStyle
* @param className cell Class
* @param mcellMethod cell调用的方法
* @param isUserNib 是否使用nib
*
* @return cell
*/
-(instancetype)initWithFrame:(CGRect)frame
style:(UITableViewStyle)style
cellWithClassName:(NSString *)className
cellWithMethod:(NSString *)cellMethod
cellWithIsUserNib:(BOOL)isUserNib;

// 数据列表
@property (nonatomic, strong) NSMutableArray *dataList;

@property (nonatomic, copy) TableBlock tableBlock;

// 数据行选择时所触发的block
- (void)blockWithCellSelectRowAtIndexPath:(TableBlock)tableBlock;

@end

.m
//
// PublicTableView.m
// JointCrm
//
// Created by Mac on 15/10/27.
// Copyright © 2015年 Mac. All rights reserved.
//

#import "PublicTableView.h"
#import <objc/objc-runtime.h>

@interface PublicTableView()<UITableViewDataSource,UITableViewDelegate>{
NSString *cellClassName;
NSString *cellInvokMethod;
}
@end

@implementation PublicTableView

-(instancetype)initWithFrame:(CGRect)frame style:(UITableViewStyle)style cellWithClassName:(NSString *)className cellWithMethod:(NSString *)cellMethod cellWithIsUserNib:(BOOL)isUserNib{

if (self = [super initWithFrame:frame style:style]) {

self.delegate = self;
self.dataSource = self;

cellClassName = className;
cellInvokMethod = cellMethod;

if (isUserNib) {
[self registerNib:[UINib nibWithNibName:className bundle:nil] forCellReuseIdentifier:className];
}else{
[self registerClass:NSClassFromString(className) forCellReuseIdentifier:className];
}
}

return self;
}

- (NSInteger)tableView:(UITableView *)tableView numberOfRowsInSection:(NSInteger)section{
return self.dataList.count;
}

- (UITableViewCell *)tableView:(UITableView *)tableView cellForRowAtIndexPath:(NSIndexPath *)indexPath{
UITableViewCell *cell = [tableView dequeueReusableCellWithIdentifier:cellClassName forIndexPath:indexPath];

if ([cell respondsToSelector:NSSelectorFromString(cellInvokMethod)]) {

id value = self.dataList[indexPath.row];

[self sendMsg:cell method:NSSelectorFromString(cellInvokMethod) value1:value value2:indexPath];

}else{
DLog(@"%@",@"cell未实现该方法");
}

return nil;
}

- (void)tableView:(UITableView *)tableView didSelectRowAtIndexPath:(NSIndexPath *)indexPath{

self.tableBlock(tableView,indexPath);
}

-(void)blockWithCellSelectRowAtIndexPath:(TableBlock)tableBlock{
self.tableBlock = tableBlock;
}

- (void)sendMsg:(id)rever method:(SEL)method value1:(id)value1 value2:(id)value2{

int (*action)(id, SEL, id, id) = (int (*)(id, SEL, id, id)) objc_msgSend;

action(rever, method, value1, value2);
}

@end

使用:
PublicTableView *publicTableView = [[PublicTableView alloc] initWithFrame:self.view.frame
style:UITableViewStylePlain
cellWithClassName:@"MyCell"
cellWithMethod:@"cellConfig:"
cellWithIsUserNib:YES];

[self.view addSubview:publicTableView];

publicTableView.dataList = dataList;

[publicTableView blockWithCellSelectRowAtIndexPath:^(UITableView *tableView, NSIndexPath *indexPath) {

}];


注意:这里只需要把每个cell的类名,及配置数据的方法传进去就可以了
  • 0
    点赞
  • 0
    收藏
    觉得还不错? 一键收藏
  • 0
    评论

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

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

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

抵扣说明:

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

余额充值