iOS—简单封装UITableView使用1

  如果项目用到很多的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:
  • 0
    点赞
  • 0
    收藏
    觉得还不错? 一键收藏
  • 0
    评论

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

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

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

抵扣说明:

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

余额充值