前几天我说过简单的UITableView 封装,是基于本地数据的情况下,今天我来说说网络请求下,我们封装tableView,其中我用的AFNetWorking 3.0 网络请求 跟新版的MJ 刷新。其中数据的Model 跟tableViewCell 我们代码不变还是用 上次说的文章代码。
首先说说我的思路,首先我们定义一个类MyTableView 继承UIView;给他一个初始化方法(包括URL链接的字符串):例如:
<span style="color:#cc0000;">-(instancetype)initWithFrame:(CGRect)frame URL:(NSString *)url cellname:(NSString *)cellname model:(NSString *)model data:(NSString *)data style:(UITableViewStyle )style;</span>
我们在给他定义几个属性 1个是否支持上拉 加载 一个是否支持下拉刷新 在给他定义一个头部View 在给他一个背景图片数字 可以定义个数字类型 数字1就去第一张占位图片 <span style="color:#cc0000;">@property(nonatomic,assign)BOOL refresh;
@property(nonatomic,assign)BOOL scrollEnabled;
@property(nonatomic,assign)int indexstyle;</span>
当然也可以多定义些数据属性
在定义几个方法 1个创建 一个下载数据
<span style="color:#cc0000;">-(void)getdownDatas;
-(void)createTableView;</span>
我们也可以定义几个代理 把TableView上面的事件,传到外面去 。
现在上全部代码。具体代码还要整理,主要下载的数据解析。
这里是.H文件的代码
<span style="color:#ff0000;">@class MyTableView;
@protocol MyTableViewDelegate <NSObject>
@optional
//cell点击事件
-(void)didcell:(MyTableView *)view modelid:(HttpDownModel *)model fromindex:(int)index;
//刷新事件
-(void)didshuaxin:(int)index page:(int)page; //1是上 2是下
//cell中某个按钮呗点击
-(void)didCELL:(NSString *)orderCode selectanniu:(UIButton *)btn Dingdancell:(HttpDownModel *)model cell:(MyTableViewCell *)cell;
//cell中某个按钮呗点击
-(void)didcell:(MyTableViewCell *)cell buttbn:(UIButton *)btn;
-(void)didCelltiaoshi;
@end
@interface MyTableView : UIView
//网络地址请求数据
-(instancetype)initWithFrame:(CGRect)frame URL:(NSString *)url cellname:(NSString *)cellname model:(NSString *)model data:(NSString *)data style:(UITableViewStyle )style;
//下载数据的url;
@property(nonatomic,copy)NSString *url;
//cell的名称
@property(nonatomic,copy)NSString *cellname;
//数据model
@property(nonatomic,copy)NSString *model;
//下载数据取字典的data
@property(nonatomic,copy)NSString *data;
//tableview的样式
@property(nonatomic,assign)UITableViewStyle style;
//刷新page
@property(nonatomic,assign)int page;
//判断是否添加刷新
@property(nonatomic,assign)BOOL refresh;
//判断tbaleview是否可以拖动
@property(nonatomic,assign)BOOL scrollEnabled;
//无数据的展位图片
@property(nonatomic,assign)int indexstyle;
//1,购买记录无图片
@property(nonatomic,retain)UIImageView *ZhanweiView;
//tableView 无数据的占位图片
@property(nonatomic,retain)UIImage *ZhanweiImage;
@property(nonatomic,assign)UITableViewCellSelectionStyle selectionStyle1;
@property(nonatomic,assign)id<MyTableViewDelegate>delegate;
//tableView的头部view
@property(nonatomic,retain)UIView *TableHeadView;
//下载数据
-(void)getdownDatas;
-(void)createTableView;
- (UITableViewCell *)tableView:(UITableView *)tableView cellForRowAtIndexPath:(NSIndexPath *)indexPath;</span>
这里是.M代码
<span style="color:#ff0000;">#define WallPaperID @"WallPaperID"
@interface MyTableView ()<UITableViewDataSource,UITableViewDelegate,MyTableViewCellDelegate>
//TableView布局
@property (nonatomic, retain) UITableView *TableView;
//UICollectionView数据
@property(nonatomic,retain)NSMutableArray *arrayDatas;
@property(nonatomic,copy)NSString *urlPath;
@property(nonatomic,retain)UIActivityIndicatorView *actView;
@end
@implementation MyTableView
-(instancetype)initWithFrame:(CGRect)frame URL:(NSString *)url cellname:(NSString *)cellname model:(NSString *)model data:(NSString *)data style:(UITableViewStyle )style
{
_cellname=cellname;
_model=model;
_data=data;
_page=1;
_url=url;
_style=style;
_scrollEnabled=YES;
_refresh=YES;
_arrayDatas=[NSMutableArray array];
if (self=[super initWithFrame:frame]) {
}
return self;
}
-(void)createTableView
{
_TableView=[[UITableView alloc]initWithFrame:self.bounds style:_style];
_TableView.delegate=self;
_TableView.dataSource=self;
if (_url.length>0) {
_TableView.separatorStyle = UITableViewCellSeparatorStyleNone;
_urlPath=[NSString stringWithFormat:@"%@&page=%d",_url,_page];
}
_TableView.showsVerticalScrollIndicator = NO;
[self addSubview:_TableView];
_TableView.separatorStyle = UITableViewCellSeparatorStyleNone;
_TableView.scrollEnabled=_scrollEnabled;
if (_TableHeadView) {
_TableView.tableHeaderView=_TableHeadView;
}
// 下拉刷新
self.TableView.mj_header= [MJRefreshNormalHeader headerWithRefreshingBlock:^{
if ([self.delegate respondsToSelector:@selector(didshuaxin:page:)]) {
[self.delegate didshuaxin:1 page:_page];
}
_page=1;
[self getdownDatas];
// 结束刷新
[self.TableView.mj_header endRefreshing];
}];
self.TableView.mj_header.automaticallyChangeAlpha = YES;
// 上拉刷新
self.TableView.mj_footer = [MJRefreshBackNormalFooter footerWithRefreshingBlock:^{
if ([self.delegate respondsToSelector:@selector(didshuaxin:page:)]) {
[self.delegate didshuaxin:2 page:_page];
}
_page++;
// _urlPath=[NSString stringWithFormat:@"%@&page=%d",_url,_page];
[self getdownDatas];
[self.TableView.mj_footer endRefreshing];
}];
}
#pragma mark---下载数据
-(void)getdownDatas
{
[self createShuaxin]; //刷新小控件
_urlPath=[NSString stringWithFormat:@"%@&page=%d",_url,_page];
AFHTTPSessionManager *manager = [AFHTTPSessionManager manager];
//设置返回类型为二进制
manager.responseSerializer = [AFHTTPResponseSerializer serializer];
[manager GET:_urlPath parameters:nil progress:^(NSProgress * _Nonnull downloadProgress) {
} success:^(NSURLSessionDataTask * _Nonnull task, id _Nullable responseObject) {
NSDictionary *responseDict = [NSJSONSerialization JSONObjectWithData:responseObject options:NSJSONReadingMutableContainers error:nil];
[self duxu:responseDict];
} failure:^(NSURLSessionDataTask * _Nullable task, NSError * _Nonnull error) {
NSLog(@"下载出错=%@",error);
}];
self.actView.alpha=0;
}
#pragma mark---读取数据 //这里要自己解析数据 跟后台商量好
-(void)duxu:(NSDictionary *)dict
{
if (_page==1) {
[_arrayDatas removeAllObjects];
}
Class cls = NSClassFromString(_model);
NSArray *array=[dict objectForKeyedSubscript:_data];
for (int i=0; i<array.count; i++) {
HttpDownModel *model2342=[[cls alloc]initWithdict:array[i]];
[_arrayDatas addObject:model2342];
}
if (_arrayDatas.count==0) {
if (_ZhanweiView) {
_ZhanweiView.alpha=1;
}else {
[self zhanwei];
}
}else
{
_ZhanweiView.alpha=0;
}
[_TableView reloadData];
}
-(void)zhanwei
{
if (!_ZhanweiView) {
_ZhanweiView=[[UIImageView alloc]initWithFrame:CGRectMake(W_SELFXX/2-75, self.bounds.size.height/2-75, 150, 150)];
_ZhanweiView.image=_ZhanweiImage;
[_TableView addSubview:_ZhanweiView];
}
}
- (NSInteger)tableView:(UITableView *)tableView numberOfRowsInSection:(NSInteger)section
{
return self.arrayDatas.count;
}
- (UITableViewCell *)tableView:(UITableView *)tableView cellForRowAtIndexPath:(NSIndexPath *)indexPath
{
Class cls = NSClassFromString(_cellname);
MyTableViewCell * cell=[tableView dequeueReusableCellWithIdentifier:WallPaperID];
if (cell==nil) {
cell=[[cls alloc]initWithStyle:UITableViewCellStyleDefault reuseIdentifier:WallPaperID];
cell.frame=self.frame;
cell.Delegate=self;
cell.model=self.arrayDatas[indexPath.row];
cell.selectionStyle = _selectionStyle1;
}
return cell;
}
-(CGFloat)tableView:(UITableView *)tableView heightForRowAtIndexPath:(NSIndexPath *)indexPath
{
HttpDownModel *model=_arrayDatas[indexPath.row];
if (model.cellH) {
return model.cellH;
}
return 120;
}
#pragma mark-----cell被点击
-(void)tableView:(UITableView *)tableView didSelectRowAtIndexPath:(NSIndexPath *)indexPath
{
[tableView deselectRowAtIndexPath:indexPath animated:YES];
HttpDownModel *model=self.arrayDatas[indexPath.row];
if ([self.delegate respondsToSelector:@selector(didcell:modelid:fromindex:)]) {
[self.delegate didcell:self modelid:model fromindex:(int)indexPath.row];
}
}
-(void)didCELL:(NSString *)orderCode selectanniu:(UIButton *)btn Dingdancell:(HttpDownModel *)model cell:(MyTableViewCell *)cell
{
if ([self.delegate respondsToSelector:@selector(didCELL:selectanniu:Dingdancell:cell:)]) {
[self.delegate didCELL:orderCode selectanniu:btn Dingdancell:model cell:cell];
}
}
-(void)didcell:(MyTableViewCell *)cell buttbn:(UIButton *)btn
{
if ([self.delegate respondsToSelector:@selector(didcell:buttbn:)]) {
[self.delegate didcell:cell buttbn:btn];
}
}
#pragma mark---创建刷新小按钮
-(void)createShuaxin
{
if (!self.actView) {
self.actView=[Helper justshuaxinViewAfram:CGRectMake(self.bounds.size.width/2-5,self.bounds.size.height/2-5 , 10, 10)];
[self.TableView addSubview:self.actView];
}else
{
self.actView.alpha=1;
}
}
</span>
如果对你有帮助,请记得回来帮忙点赞