工作中,总会遇到各种的列表页面,大家基本上都是使用UITableView来完成,最基本的功能有:上拉加载、下拉刷新、多个Cell展示、空视图展示、网络错误视图展示。
如果一个项目中有20个列表页,那么是在每一个列表页面中都写入这些功能代码和逻辑吗????
开发中,为了节省列表页面的开发时间,把这些功能代码和业务逻辑全部写在了一个BaseTbaleViewController中,这样能够做到,只需要给我传入一个模型数组,就可以直接实现整个列表页面。
controller中的处理
image.png首先声明一个controller继承于BTZBaseTbaleViewController
在BTZTestTableViewController中
image.png
网络请求,需要传入url和参数
在controller重写getTableRequestUrl和getTableRequestParamer,并传入相应的参数
- (NSString *)getTableRequestUrl {
return @"这里填写网络请求的URL";
}
- (NSDictionary *)getTableRequestParamer {
//这里填写参数
NSMutableDictionary *dict = [NSMutableDictionary dictionary];
[dict setValue:@(self.currentPage) forKey:@"currentPage"];
[dict setValue:@(self.pageSize) forKey:@"pageSize"];
[dict setValue:@2 forKey:@"type"];
return dict;
}
- (void)loadNewData {
//1.loading
//2.调用这个方法
[self loadNewDataWithRequest:^(NSString *url, NSDictionary *parameters) {
__weak typeof(self) weakSelf = self;
//这里使用网络请求
// [HSHTTPClient request:POST URLString:url parameters:parameters success:^(id json) {
// NSString *code = json[@"code"];
// NSString *msg = json[@"msg"];
// NSDictionary *result = json[@"result"];
// if (code.intValue == 100000) {
// NSArray *array = [result objectForKey:@"data"];
// if (array.count > 0) {
// NSArray *dataArray = [BTZTestItem mj_objectArrayWithKeyValuesArray:[result objectForKey:@"data"]];
// [weakSelf serviceWithResult:dataArray operation:nil];
// }
//
// }else {
//
// }
//
//
// } failure:^(NSError *error) {
// [self serviceFailedWithError:error operation:nil];
// }];
NSMutableArray *array = [[NSMutableArray alloc] init];
for (int i = 0; i< 10; i ++) {
BTZTestItem *item = [[BTZTestItem alloc] init];
if (i % 2 == 0) {
item.type = 1;
} else {
item.type = 2;
}
[array addObject:item];
}
[weakSelf serviceWithResult:array operation:nil];
}];
}
模型数据处理
image.png模型也要继承于BTZBaseItem
如果是单个Cell的页面
单个cell页面,只需要在item中的init方法中传入 cellName、isXibCell、cellHeight
- (instancetype)init
{
self = [super init];
if (self) {
self.cellName = @"传入cell的类名";
self.cellHeight = 100;//cell高度
self.isXibCell = NO;//是否是XIB
}
return self;
}
如果是多个cell的页面
多个cell的页面,可以设定一个type值来进行赋值
- (void)setType:(int)type {
switch (type) {
case 1:
{
self.titleStr = @"这是XIB";
self.cellName = @"BTZTestXibTableViewCell";
self.cellHeight = 50;
self.isXibCell = YES;
}
break;
case 2:
{
self.titleStr = @"这是纯代码";
self.cellName = @"BTZTestTableViewCell";
self.cellHeight = 60;
self.isXibCell = NO;
}
break;
default:
break;
}
}
cell处理
image.pngcell继承于BTZBaseTableViewCell
使用XIB的cell只需要实现updateCell方法,在里面赋值
- (void)updateCell:(BTZTestItem *)item {
self.titleLabel.text = item.titleStr;
}
纯代码cell实现
- (instancetype)initWithStyle:(UITableViewCellStyle)style reuseIdentifier:(NSString *)reuseIdentifier {
self = [super initWithStyle:style reuseIdentifier:reuseIdentifier];
if (self) {
[self creatView];
self.backgroundColor = [UIColor redColor];
}
return self;
}
- (void)creatView {
self.titleLabel = [[UILabel alloc] initWithFrame:CGRectMake(0, 0, 100, 50)];
[self.contentView addSubview:self.titleLabel];
}
- (void)updateCell:(BTZTestItem *)item {
self.titleLabel.text = item.titleStr;
}
完成后,网络请求、下拉刷新、上拉加载、空视图、网络错误视图这些业务逻辑就不用再处理了
网友评论