.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的类名,及配置数据的方法传进去就可以了
一个公共的TableView,然后不会为每个TableView加delegate和datasource
猜你喜欢
转载自wfkbyni.iteye.com/blog/2252384
今日推荐
周排行