如果项目用到很多的UITableView的时候,如果我们每个页面都去新构建UITableView,那么的我们项目的代码量会很大,特别是网络请求下来的数据,重复的创建,解析数据。
下面我谈谈个人在项目使用UITableView的时候,我会简单的封装一个UITableView,使项目中只有一个UITableView,减少代码量,下面我就来说说的想法.
1.首先我先在没有网络请求的情况下创建自定义UITableView,那我只要封装一个方法,只要你提供数据 ,跟数据模型的实例名称,还有Cell的实例名称。(网络请求也是一个原理,后面我还会把代码弄出来).
我们定义个MyTableView继承UIView;
中间包括2各代理方法,当然也可以在添加代理。
@protocol MyTableViewDelegate <NSObject>
@optional
/****
CELL 点击的返回
*/
-(void)MytableView:(MyTableView *)tableView model:(nullable MyTableViewModel *)myTableViewModel cell:(nullable MyTableViewCell *)cell;
/***
错误信息
*/
-(void)MytableView:(MyTableView *)tableView Myerror:(NSString *)Myerror;
@end
@interface MyTableView : UIView //代理
@property(nonatomic,assign)id<MyTableViewDelegate> delegate;
@property(nonatomic,assign)id<MyTableViewCellDelegate>MytableViewCelldelegate;
-(instancetype)initWithFrame:(CGRect)frame ArrayDatas:(NSArray *)arrayDatas;
-(instancetype)initWithFrame:(CGRect)frame ArrayDatas:(NSArray *)arrayDatas index:(int)index;
-(instancetype)initWithFrame:(CGRect)frame ArrayDatas:(NSArray *)arrayDatas index:(int)index modelname:(NSString *)modelname cellname:(NSString *)cellname;
//创建tableView
-(void)createTableView;
//tableView的数组
@property(nonatomic,retain)NSArray *arrayDatas;
//tableView
@property(nonatomic,retain)UITableView *tableView;
//自定义的Mymodel
@property(nonatomic,copy)NSString *MyModelname;
//自定义的Mycell
@property(nonatomic,copy)NSString *MyCellname;
//TableView的头部View
@property(nonatomic,retain)NSArray<UIView *> *HeaderViews;
//尾部的View
@property(nonatomic,retain)NSArray<UIView *> *FooterViews;
下面是.M的代码
@interface MyTableView ()
@property(nonatomic,assign)Class modelcls;
@property(nonatomic,assign)Class cellcls;
@end
@interface MyTableView ()<UITableViewDelegate,UITableViewDataSource>
@property(nonatomic,assign)int index;
@end
@implementation MyTableView
-(instancetype)initWithFrame:(CGRect)frame ArrayDatas:(NSArray *)arrayDatas index:(int)index{
if (self=[self initWithFrame:frame ArrayDatas:arrayDatas]) {
self.index=index;
}
return self;
}
-(instancetype)initWithFrame:(CGRect)frame ArrayDatas:(NSArray *)arrayDatas{
if (self=[super initWithFrame:frame]) {
self.arrayDatas=[NSArray arrayWithArray:arrayDatas];
self.index=0;
}
return self;
}
-(instancetype)initWithFrame:(CGRect)frame ArrayDatas:(NSArray *)arrayDatas index:(int)index modelname:(NSString *)modelname cellname:(NSString *)cellname{
if (self=[self initWithFrame:frame ArrayDatas:arrayDatas index:index]) {
self.MyModelname=modelname;
self.MyCellname=cellname;
}
return self;
}
-(void)setMyCellname:(NSString *)MyCellname{
_MyCellname=MyCellname;
_cellcls=NSClassFromString(MyCellname);
}
-(void)setMyModelname:(NSString *)MyModelname{
_MyModelname=MyModelname;
_modelcls=NSClassFromString(MyModelname);
}
-(void)createTableView{
self.tableView=[[UITableView alloc]initWithFrame:self.bounds style:UITableViewStylePlain];
self.tableView.delegate=self;
self.tableView.dataSource=self;
self.tableView.separatorStyle = UITableViewCellSeparatorStyleNone;
self.tableView.bounces=NO;
[self addSubview:self.tableView];
}
- (NSInteger)numberOfSectionsInTableView:(UITableView *)tableView{
return (self.index==0)?1:self.arrayDatas.count;
}
- (NSInteger)tableView:(UITableView *)tableView numberOfRowsInSection:(NSInteger)section{
if (self.index==0) {
return self.arrayDatas.count;
}else {
NSArray *array=(NSArray *)self.arrayDatas[section] ;
return array.count;
}
}
- (UITableViewCell *)tableView:(UITableView *)tableView cellForRowAtIndexPath:(NSIndexPath *)indexPath{
MyTableViewCell *cell=[tableView dequeueReusableCellWithIdentifier:[NSString stringWithFormat:@"cell%ld%lu",(long)indexPath.row,(unsigned long)indexPath.length]];
if (!cell) {
cell=[[_cellcls alloc]initWithStyle:UITableViewCellStyleDefault reuseIdentifier: