测试已测试 | ✗ |
语言语言 | Obj-CObjective C |
许可证 | MIT |
发布最后发布 | 2016年3月 |
由ansun维护。
依赖 | |
MJRefresh | >= 0 |
AFNetworking | = 2.6.3 |
JSONModel | >= 0 |
一个可以极大简化 UitableView 操作的第三方框架
UITableView 可谓是 iOS 开发中的重中之重,熟练掌握 TableView 的程度在很大程度上决定了您对 UI 界面的熟悉程度。不过当逻辑复杂时,操作 UitableView 仍然是一件挺麻烦的事情,因此这个框架旨在极大程度地减轻 UitableView 的复杂度,让程序员们可以更多地考虑逻辑问题,而不是界面的调整。
1.自动加载数据远程 URL 的 JSON 数据,对于 section 的解析也不例外,让用户用更少的代码获得更高的效率。
2.可以手动创建本地的静态 cell,可以自由控制 section 和 cell 的所有操作。
3.自定义的 HUD 动画可以在加载数据时毫无违和感。
1.简单方便,将大量繁琐的操作封装起来,用户只需几行代码即可获取一个可以抓取远程数据的 TableView。
2.使用 delegate 模式,这样可以将 cell、view controller 和 data 分离开来,一个 cell 绑定一个 data item,使用 ORM 将远程的 JSON 数据转换为本地对象。
3.本地静态 cell 可以使用 block 创建,并且设置点击事件。
4.可以绑定多个 cell 和 data,根据 data 数据源中数据的不同在不同 cell 之间自由切换,满足复杂的列表需求。
5.支持 Xib 和纯代码两种 cell 创建方式,框架自动识别创建。
1.我使用了 AFNetworking、MJRefresh、JsonModal 三个框架来简化代码的编写,如果用户没有使用 CocoaPods,无需任何改动,只需将 LazyTableView 文件夹拖入项目即可。
2.如果用户使用了 CocoaPods,则需要在 podfile 中加入以下代码:pod "LazyTableView" 即可。
1.远程 URL 抓取:
拖入一个 UitableView,将 custom class 更改为 LazyTableView,将控件变量连接到 table1,在 .h 文件中引入 LazyTableViewDelegate 协议,然后在 view controller 中写入如下代码:
[_table1 registarCell:@"InfoCell" StrItem:@"InfoItem"];
[_table1 setDelegateAndDataSource:self];
//[_table1 setPageParam:@"pi" Page:2];
[_table1 disablePage];
[_table1 reloadRequest:@"http://v5.pc.duomi.com/search-ajaxsearch-searchall" Param:@{
@"kw":@"爱情",
@"pz":@10
}];
-(NSArray*)LazyTableViewDidFinishRequest:(LazyTableView *)tableview Request:(NSDictionary *)dic
{
if(tableview==_table1)
{
return dic[@"albums"];
}
return nil;
}
建立 InfoCell 类,继承自 LazyTableCell,将 InfoCell 的重用 id 设置为 InfoCell,写入如下代码:
-(NSNumber*)LazyTableCellHeight:(id)item Path:(NSIndexPath *)path
{
return @80;
}
-(void)LazyTableCellForRowAtIndexPath:(id)item Path:(NSIndexPath *)path
{
InfoItem *data=item;
_lbName.text=data.name;
_lbType.text=data.type;
_lbDate.text=data.release_date;
}
建立 InfoItem 类,继承自 LazyTableBaseItem,在 .h 文件中写入属性(这里的名称要与 JSON 数据中字段的名称一致,具体可参考 JsonModal 的用法):
@property (strong,nonatomic) NSString *name;
@property (strong,nonatomic) NSString *release_date;
@property (strong,nonatomic) NSString *type;
2.本地自定义静态 cell 创建,建立TableView过程如上所述,不需要建立 LazyTableBaseItem 的子类,在 view controller 中写入如下代码:
[_table2 setDelegateAndDataSource:self];
[_table2 registarCell:@"InfoCell" StrItem:nil];
[_table2 addStaticCell:80 CellBlock:^(id cell) {
InfoCell *cl=cell;
cl.lbName.text=@"dsf";
cl.lbType.text=@"dfwwfew";
} ClickBlock:^(id cell) {
NSLog(@"123");
}];
LazyTableBaseSection *sec=[[LazyTableBaseSection alloc] init];
sec.headerHeight=100;
sec.titleHeader=@"sxsx";
[_table2 addSection:sec];
[_table2 addStaticCell:80 CellBlock:^(id cell) {
InfoCell *cl=cell;
cl.lbName.text=@"123";
cl.lbType.text=@"dsfsd234";
} ClickBlock:^(id cell) {
NSLog(@"zzz");
}];
[_table2 reloadStatic];
3.本地带 section 的静态数据源的 cell 创建,其他参考上面,的在 view controller 中写入如下代码:
[_table3 setDelegateAndDataSource:self];
[_table3 registarCell:@"myTableViewCell" StrItem:@"myTableViewItem"];
NSArray* arr=@[
@{@"we":@"121312",@"data":@[
@{
@"scoreRealName":@"水电费水电费的"
},
@{
@"scoreRealName":@"水sad电费水电费的"
},
@{
@"scoreRealName":@"asdas水电费水电费的"
}
]},
@{@"we":@"121dasas312",@"data":@[
@{
@"scoreRealName":@"水电费的"
},
@{
@"scoreRealName":@"水s费水电费的"
},
@{
@"scoreRealName":@"asdas水"
}
]},
@{@"we":@"123s312",@"data":@[
@{
@"scoreRealName":@"水3443电费的"
},
@{
@"scoreRealName":@"水s费esrse水电费的"
},
@{
@"scoreRealName":@"asdas水3423"
}
]},
@{@"we":@"12",@"data":@[
@{
@"scoreRealName":@"水gfhf电费的"
},
@{
@"scoreRealName":@"水s费水电hrt费的"
},
@{
@"scoreRealName":@"asdas水7867j"
}
]}
];
NSArray *arr1=@[
@"a",
@"b",
@"c",
@"d"
];
[_table3 addDataSource:arr];
[_table3 setSectionIndexTitles:arr1];
[_table3 reloadStatic];
-(LazyTableBaseSection*)LazyTableViewInfoForSection:(LazyTableView *)tableview Request:(NSDictionary *)dic
{
if(tableview==_table3)
{
LazyTableBaseSection *sec=[[LazyTableBaseSection alloc] init];
sec.titleHeader=dic[@"we"];
sec.headerHeight=50;
sec.data=@"data";
return sec;
}
return nil;
}
4.抓取远程 URL 数据的带 section 的 cell,具体过程如3所示,不同的是需要在 LazyTableViewDidFinishRequest 中返回 section 信息所标示的字段名称,且 tableView 的初始化参照 1 的初始化方式。
5.本地自定义静态 cell 的创建(不使用 block),文件的建立如 1 所示,在 view controller 中写入如下代码:
[_tableMain setDelegateAndDataSource:self];
[_tableMain registarCell:@"MemberOptionCell" StrItem:@"MemberOptionItem"];
CustomTableBaseSection *sec=[[CustomTableBaseSection alloc] init];
MemberOptionItem *item=[[MemberOptionItem alloc] init];
item.content=@"缴纳保证金";
item.image=@"finance";
[sec addRow:item];
item=[[MemberOptionItem alloc] init];
item.content=@"消息";
item.image=@"message";
[sec addRow:item];
item=[[MemberOptionItem alloc] init];
item.content=@"公告";
item.image=@"notice";
[sec addRow:item];
item=[[MemberOptionItem alloc] init];
item.content=@"系统设置";
item.image=@"about";
[sec addRow:item];
[_tableMain addSection:sec];
[_tableMain reloadStatic];
6.多个 cell 之间的切换。调用 registerCell 即可,需要实现 LazyTableViewSwitchCell 方法
[_table1 registarCell:@"InfoCell" StrItem:@"InfoItem"];
[_table1 registarCell:@"InfoCell1" StrItem:@"InfoItem"];
[_table1 setDelegateAndDataSource:self];
[_table1 setPageParam:@"pi" Page:2];
//[_table1 disablePage];
[_table1 reloadRequest:@"http://v5.pc.duomi.com/search-ajaxsearch-searchall" Param:@{
@"kw":@"爱情",
@"pz":@10
}];
-(NSArray*)LazyTableViewDidFinishRequest:(LazyTableView *)tableview Request:(NSDictionary *)dic
{
if(tableview==_table1)
{
return dic[@"albums"];
}
return nil;
}
-(NSString*)LazyTableViewSwitchCell:(LazyTableView *)tableview Request:(NSDictionary *)item Section:(NSInteger)section Row:(NSInteger)row
{
if(tableview==_table1)
{
if([item[@"type"] isEqualToString:@"专辑"])
{
return @"InfoCell1";
}
else
{
return @"InfoCell";
}
}
return nil;
}
大致的用法如上所述,大家可以下载 demo 测试,代码里都有详细的注释,这个框架我会不断完善,自己的 app 也在使用,希望大家有什么意见和想法可以多多交流。
QQ 群:1群:460483960(目前已满) 2群:239309957,这是我们的 iOS 项目开发者 QQ 群,这是一个纯粹的 iOS 开发者社区,里面汇聚了众多有经验的 iOS 开发者,没有 HR 和打扰和广告的骚扰,为您创造一个干净的技术交流环境,如果您对我的项目以及对 iOS 开发有任何疑问,都可以加群交流,欢迎您的加入~
微信公众号:fuckingxcode 欢迎大家关注,我们群的活动投票和文章等都会在公众号里,群期刊目前也移到公众号里。