UIKit框架(21)UITableView实现复杂单元格(二)
继续介绍第二种实现复杂单元格的方式 ---- 使用storyboard中的prototype cell
prototype Cell介绍
在storyboard或xib中使用tableView时,可以添加prototype cells,其好处是:
1)可以直接进行自定义cell的设置
2)通过设置cell的重用ID,在调用出队列方法时如果没有可重用的则创建一个新的
注:一个tableView中可以设计多个prototypecell,重用ID不同即可
使用prototype cell 设计tableView是最常用的一种方式!
使用prototype cell相比较于纯代码,可以简化两处编程
简化cell获取的代码
使用了prototype cell之后,从tableView的重用队列获取指定ID的cell,如果获取不出来,会自动创建一个相同ID的prototype cell的副本,不再需要alloc+init进行创建
//1.1实现类方法,获取cell+(AMHeroCell*)cellWithTableView:(UITableView*)tableView{AMHeroCell*cell=[tableViewdequeueReusableCellWithIdentifier:@"cell"];returncell;}
简化cell的子视图设定
使用storyboard设计cell内部的子视图,也就是说不需要在代码中创建子视图并进行自动布局,这些工作全部可以在storyboard中快速完成。
也就是说,上篇文章中,介绍的1.2 1.3这两步不再需要
案例:模拟实现大众点评的首页
prototype cell是我们实现复杂tableView的一种快速开发手段
如实现如下截图的效果:
可以看到:
三个section,每个section中的cell样式不同及个数不同
第一个section:一个cell,scrollView,内部有很多按钮
第二个section:两个cell,活动信息cell
第三个section:很多个cell,产品信息cell
1)在storyboard设计出这三种cell
每种cell设定不同的ID
3)创建每个cell需要显示的数据模型类
4)每种cell分别关联UITableViewCell的子类
子类内部分别实现三步操作(见UIKit框架(21)UITableView实现复杂单元格(一))
5)控制器管理所有的数据模型并实现数据源、代理方法
#pragmamark-tableView-(NSInteger)numberOfSectionsInTableView:(UITableView*)tableView{return3;}-(NSInteger)tableView:(UITableView*)tableViewnumberOfRowsInSection:(NSInteger)section{if(section==0){return1;}elseif(section==1){returnself.activityArray.count;}elseif(section==2){returnself.goodsArray.count;}else{return0;}}-(UITableViewCell*)tableView:(UITableView*)tableViewcellForRowAtIndexPath:(NSIndexPath*)indexPath{if(indexPath.section==0){return[AMFunctionSelectionCellcellWithTableView:tableView];}elseif(indexPath.section==1){return[AMActivityCellcellWithTableView:tableView];}elseif(indexPath.section==2){return[AMGoodsInfoCellcellWithTableView:tableView];}else{returnnil;}}-(CGFloat)tableView:(UITableView*)tableViewheightForRowAtIndexPath:(NSIndexPath*)indexPath{if(indexPath.section==0){return[AMFunctionSelectionCellcellHeight];}elseif(indexPath.section==1){return[AMActivityCellcellHeight];}elseif(indexPath.section==2){return[AMGoodsInfoCellcellHeight];}else{return0;}}-(void)tableView:(UITableView*)tableViewwillDisplayCell:(UITableViewCell*)cellforRowAtIndexPath:(NSIndexPath*)indexPath{if(indexPath.section==0){}elseif(indexPath.section==1){((AMActivityCell*)cell).activityModel=self.activityArray[indexPath.row];}elseif(indexPath.section==2){((AMGoodsInfoCell*)cell).goodsInfoModel=self.goodsArray[indexPath.row];}}
案例扩展:加载更多
在最后一个section下面增加一个加载更多按钮
点击后,可以模拟一个简单的增加cell的操作
1)使用xib设计一个这个底部视图
2)关联一个UIView的子类并提供类方法快速创建
+(AMGoodsInfoFooterView*)view{return[[[NSBundlemainBundle]loadNibNamed:@"AMGoodinfoFooterView"owner:niloptions:nil]lastObject];}
3)控制器实现tableView的数据源方法
-(UIView*)tableView:(UITableView*)tableViewviewForFooterInSection:(NSInteger)section{if(section==2){AMGoodsInfoFooterView*v=[AMGoodsInfoFooterViewview];v.delegate=self;returnv;}else{returnnil;}}-(CGFloat)tableView:(UITableView*)tableViewheightForFooterInSection:(NSInteger)section{if(section==2){return40;}else{return5;}}-(CGFloat)tableView:(UITableView*)tableViewheightForHeaderInSection:(NSInteger)section{return5;}
说明:两处返回5的目的是让三个section之间的空隙不要太大
4)加载更多按钮被点击时,将点击产生这个时间通过代理传递给控制器
//UIView子类提出代理协议并添加代理属性@protocolAMGoodsInfoFooterViewDelegate<NSObject>@optional-(void)footerViewLoadMoreGoods;@end@interfaceAMGoodsInfoFooterView:UIView+(AMGoodsInfoFooterView*)view;@property(nonatomic,weak)id<AMGoodsInfoFooterViewDelegate>delegate;@end
5)控制器成为代理并实现代理方法
成为代理的操作在3)中已有
代理方法的实现:(简单模拟)
-(void)footerViewLoadMoreGoods{AMGoodsInfoModel*model=[[AMGoodsInfoModelalloc]init];model.icon=self.goodsArray[0].icon;model.goodTitle=@"东软食堂";model.goodFooter=@"已售999999999";model.goodDesc=@"吃了你就知道了";model.priceValue=@"8";[self.goodsArrayaddObject:model];NSIndexSet*s=[NSIndexSetindexSetWithIndex:2];[self.tableViewreloadSections:swithRowAnimation:UITableViewRowAnimationLeft];}
声明:本站所有文章资源内容,如无特殊说明或标注,均为采集网络资源。如若本站内容侵犯了原著者的合法权益,可联系本站删除。