如何固定uitableviewcell详解的viewForHeaderInSection

1)">1)">1" ng-class="{current:{{currentPage==page}}}" ng-repeat="page in pages"><li class='page' ng-if="(endIndex<li class='page next' ng-if="(currentPage
相关文章阅读您所在的位置: &
UITableView的下拉功能(3)
UITableView的下拉功能(3)
清华大学出版社
《iOS高级编程》第2章Tableview进阶指南,在本章中,将学习到以超越标准实现的方式使用UITableView,并理解UITableView类的工作方式。你会创建一个聊天视图控制器,它支持定制的单元格和灵活的行高,以及下钻功能的实现,能够将多个对象的多个分类进行分组,从而生成一个高级的用户界面。最后,你会为表格视图的实现添加搜索功能。本节为大家介绍UITableView的下拉功能。
2.3& UITableView的下拉功能(3)
在YDViewController类的开头,导入了YDSectionHeaderView.h文件。请为UITableView的实例创建属性,并且创建NSInteger型的变量,用以追踪openSectionIndex变量的值。定义名为arrayOfSectionHeaders的NSMutableArray数组,用来保存所创建的YDSection- HeaderViews的实例。最后,定义名为dataDict的NSMutableDictionary类型的属性来保存数据,这些数据代表轿车的集合。完整的实现如代码清单2-21中所示。
代码清单2-21& Chapter2/YDDrillDown/YDViewController.h
#import&/UIKit.h&#import&&YDSectionHeaderView.h& &@interface&YDViewController&:&UIViewController && &@property&(nonatomic,strong)&UITableView&*mTableV &@property&(nonatomic,&assign)&NSInteger&openSectionI &@property&(nonatomic,strong)&NSMutableArray&*arrayOfSectionH &@property&(nonatomic,strong)&NSMutableDictionary&*dataD &@end&
在YDViewController类的实现里,在viewDidLoad方法中创建了表视图,并且设置了它的datasource和delegate属性。在viewDidLoad方法的最后,将其作为子视图添加到ViewControllers上,然后调用loadData方法创建和加载数据。
为了举个例说明,创建名为ford 的NSArray,用来保存YDCar类型的若干对象。创建名为chevy的另一个NSArray,用来保存若干Chevrolet(雪佛兰)牌子轿车的型号。一旦dataDict对象创建完成,那么就需要为这个NSDictionary字典中的每一个键值创建YDSectionHeaderView对象,并且将其保存在arrayOfSectionHeaders数组中。#pragma Headers语句之后的代码实现了delegate调用的相关逻辑。这段代码从arrayOfSectionHeaders数组中抽取合适的YDSectionHeaderView对象,限定section的行数,从展开的section中删除一些行,使用动画效果在UITableView中插入一些新行。完整的实现如代码清单2-22中所示。
代码清单2-22& Chapter2/YDDrillDown/YDViewControllerViewController.m
#import&&YDViewController.h& &#import&&YDCar.h& &#define&HEADER_HEIGHT&&60.0f &@interface&YDViewController&(), &&&&&&&&&UITableViewDelegate, &&&&&&&&&YDSectionHeaderViewDelegate&& &@end &&@implementation&YDViewController &&&-&(void)viewDidLoad &{ &&&&&[super&viewDidLoad]; &&&&&CGRect&appframe=[[UIScreen&mainScreen]&applicationFrame]; &&&&&&&&&//create&the&tableView &&&&&self.mTableView&=&[[UITableView&alloc]& &&&&&&&&&&&&&&&initWithFrame:appframe&style:UITableViewStylePlain]; &&&&&selfself.mTableView.dataSource= &&&&&selfself.mTableView.delegate= &&&&&self.mTableView.rowHeight&=&60; &&&&&self.mTableView.sectionHeaderHeight&=&HEADER_HEIGHT; &&&&&[self.view&addSubview:self.mTableView]; &&&&&[self&loadData]; &} &&-(void)loadData &{ &&&&&//Create&some&models&for&Ford &&&&&NSArray&*ford&=&[[NSArray&alloc]&initWithObjects: &&&&&&&&&&&&&&&&&&&&&&[[YDCar&alloc]&initWithMake:@&Ford&& &&&&&&&&&&&&&&&&&&&&&&&&&&&&&&&&&&&&&model:@&2013&F-150&& &&&&&&&&&&&&&&&&&&&&&&&&&&&&&&&&&&&&&imageName:@&.jpg&], &&&&&&&&&&&&&&&&&&&&&&[[YDCar&alloc]&initWithMake:@&Ford&& &&&&&&&&&&&&&&&&&&&&&&&&&&&&&&&&&&&&&model:@&2013&Super&Duty&& &&&&&&&&&&&&&&&&&&&&&&&&&&&&&&&&&&&&&imageName:@&2013superduty.jpg&], &&&&&&&&&&&&&&&&&&&&&&[[YDCar&alloc]&initWithMake:@&Ford&& &&&&&&&&&&&&&&&&&&&&&&&&&&&&&&&&&&&&&model:@&Shelby&GT500&& &&&&&&&&&&&&&&&&&&&&&&&&&&&&&&&&&&&&&imageName:@&shelbygt500.jpg&], &&&&&&&&&&&&&&&&&&&&&&nil]; &&&&&//create&some&models&for&Chevrolet &&&&&NSArray&*chevy&=&[[NSArray&alloc]&initWithObjects: &&&&&&&&&&&&&&&&&&&&&&&[[YDCar&alloc]&initWithMake:@&Chevrolet&& &&&&&&&&&&&&&&&&&&&&&&&&&&&&&&&&&&&&&&model:@&2013&Suburban&3/4&ton&& &&&&&&&&&&&&&&&&&&&&&&&&&&&&&&&&&&&&&&imageName:@&suburban.png&], &&&&&&&&&&&&&&&&&&&&&&&[[YDCar&alloc]&initWithMake:@&Chevrolet&& &&&&&&&&&&&&&&&&&&&&&&&&&&&&&&&&&&&&&&model:@&2012&Colorado&& &&&&&&&&&&&&&&&&&&&&&&&&&&&&&&&&&&&&&&imageName:@&colorado.jpg&], &&&&&&&&&&&&&&&&&&&&&&&nil]; &&&&&//Create&a&dictionary&to&store&them &&&&&self.dataDict&=&[[NSMutableDictionary&alloc]&init]; &&&&&[self.dataDict&setObject:ford&forKey:@&Ford&]; &&&&&[self.dataDict&setObject:chevy&forKey:@&Chevrolet&]; &&&&&//Create&an&array&of&YDSectionHeaderViews&with&the&title&and&number&of&rows &&&&&self.arrayOfSectionHeaders=[[NSMutableArray&alloc]&init]; &&&&&int&sectionNr&=&0; &&&&&for&(id&key&in&self.dataDict)&{ &&&&&&&&&id&anObject&=&[self.dataDict&objectForKey:key]; &&&&&&&&&YDSectionHeaderView&*make&=&[[YDSectionHeaderView&alloc]& &&&&&&&&&&&&&&&initWithFrame:CGRectMake(0.0,&0.0,&320,&HEADER_HEIGHT)& &&&&&&&&&&&&&&&title:key&section:sectionNr&delegate:self& &&&&&&&&&&&&&&&numrow:[anObject&count]]; &&&&&&&&&[self.arrayOfSectionHeaders&addObject:make]; &&&&&&&&&sectionNr++; &&&&&} &&&&&self.openSectionIndex&=&NSNotFound; &&&&&[self.mTableView&reloadData]; &} &#pragma&HEADER &-(UIView*)tableView:(UITableView*)tableView& &&&&viewForHeaderInSection:(NSInteger)section&{ &&&&&YDSectionHeaderView&*sectionHeaderView&&=& &&&&&&&&&&(YDSectionHeaderView&*) &&&&&&&&&&&[self.arrayOfSectionHeaders&objectAtIndex:section]; &&&&&return&sectionHeaderV &} &-(void)sectionHeaderView:(YDSectionHeaderView*)sectionHeaderView& &&&&&&&&sectionOpened:(NSInteger)sectionOpened&{ &&&&& &&&&&if&(sectionOpened&!=&NSNotFound)& &&&&&//all&sections&are&closed&so&we&can't&do&anything &&&&&&&&&{ &&&&&&&&&YDSectionHeaderView&*sectionHeaderView&=& &&&&&&&&&&&&&&&&&&&&[self.arrayOfSectionHeaders& &&&&&&&&&&&&&&&&&&&&objectAtIndex:sectionOpened]; &&&&&&&&&sectionHeaderView.expanded&=&YES; &&&&&&&&&/* &&&&&&&&&&Create&an&array&containing&the&index&paths&of&the&rows&to&insert& &&&&&&&&&&These&correspond&to&the&rows&for&each&quotation&in&the& &&&&&&&&&&current&section. &&&&&&&&&&*/ &&&&&&&&&NSInteger&countOfRowsToInsert&=&sectionHeaderView.numberOfR &&&&&&&&&NSMutableArray&*indexPathsToInsert&=&[[NSMutableArray&alloc]&init]; &&&&&&&&&for&(NSInteger&i&=&0;&i&&;&i++)&{ &&&&&&&&&&&&&[indexPathsToInsert& &&&&&&&&&&&&&&&&&&addObject:[NSIndexPath&indexPathForRow:i& &&&&&&&&&&&&&&&&&&&&&&&&&&&&&inSection:sectionOpened]]; &&&&&&&&&} &&&&&&&&&/* &&&&&&&&&&Create&an&array&containing&the&index&paths&of&the&rows&to&delete:& &&&&&&&&&&These&correspond&to&the&rows&for&each&quotation&in&the& &&&&&&&&&&previously-open&section,&if&there&was&one. &&&&&&&&&&*/ &&&&&&&&&NSMutableArray&*indexPathsToDelete&=&[[NSMutableArray&alloc]&init]; &&&&&&&&&NSInteger&previousOpenSectionIndex&=self.openSectionI &&&&&&&&&if&(previousOpenSectionIndex&!=&NSNotFound)&{ &&&&&&&&&&&&&YDSectionHeaderView&*previousOpenSectionHeaderView=& &&&&&&&&&&&&&&&&&&&&&[self.arrayOfSectionHeaders& &&&&&&&&&&&&&&&&&&&&&&objectAtIndex:previousOpenSectionIndex]; &&&&&&&&&&&&&previousOpenSectionHeaderView.expanded&=&NO; &&&&&&&&&&&&&[previousOpenSectionHeaderView&toggleOpenWithUserAction:NO]; &&&&&&&&&&&&&NSInteger&countOfRowsToDelete&=& &&&&&&&&&&&&&&&&&&&&&&&previousOpenSectionHeaderView.numberOfR &&&&&&&&&&&&&for&(NSInteger&i&=&0;&i&&;&i++)&{ &&&&&&&&&&&&&&&&&[indexPathsToDelete& &&&&&&&&&&&&&&&&&&&&addObject:[NSIndexPath&indexPathForRow:i& &&&&&&&&&&&&&&&&&&&&&&&&&&&&&&&inSection:previousOpenSectionIndex]]; &&&&&&&&&&&&&} &&&&&&&&&} &&&&&&&&& &&&&&&&&&&&&&//&Style&the&animation&so&that&there's&a&smooth&flow&in&either& &&&&&&&&&&&&&&&&direction. &&&&&&&&&UITableViewRowAnimation&insertA &&&&&&&&&UITableViewRowAnimation&deleteA &&&&&&&&&if&(previousOpenSectionIndex&==&NSNotFound&||& &&&&&&&&&&&&&sectionOpened&&)&{ &&&&&&&&&&&&&insertAnimation&=&UITableViewRowAnimationTop; &&&&&&&&&&&&&deleteAnimation&=&UITableViewRowAnimationBottom; &&&&&&&&&} &&&&&&&&&else&{ &&&&&&&&&&&&&insertAnimation&=&UITableViewRowAnimationBottom; &&&&&&&&&&&&&deleteAnimation&=&UITableViewRowAnimationTop; &&&&&&&&&} &&&&&&&&&&&&&//&Apply&the&updates. &&&&&&&&&[self.mTableView&beginUpdates]; &&&&&&&&&[self.mTableView&insertRowsAtIndexPaths:indexPathsToInsert& &&&&&&&&&&&&&&&&&&&&&&&&&&withRowAnimation:insertAnimation]; &&&&&&&&&[self.mTableView&deleteRowsAtIndexPaths:indexPathsToDelete& &&&&&&&&&&&&&&&&&&&&&&&&&&withRowAnimation:deleteAnimation]; &&&&&&&&&[self.mTableView&endUpdates]; &&&&&&&&&self.openSectionIndex&=&sectionOpened; &&&&&&&&&} &} &-(void)sectionHeaderView:(YDSectionHeaderView*)sectionHeaderView& &&&&&&&&&sectionClosed:(NSInteger)sectionClosed&{ &&&&&&&&&YDSectionHeaderView&*thissectionHeaderView&=& &&&&&&&&&&&&[_arrayOfSectionHeaders&objectAtIndex:sectionClosed]; &&&&&thissectionHeaderView.expanded&=&NO; &&&&&NSInteger&countOfRowsToDelete&=& &&&&&&&&&&&&[self.mTableView&numberOfRowsInSection:sectionClosed]; &&&&&if&(countOfRowsToDelete&&0)&{ &&&&&&&&&NSMutableArray&*indexPathsToDelete&=&[[NSMutableArray&alloc]&init]; &&&&&&&&&for&(NSInteger&i&=&0;&i&&;&i++)&{ &&&&&&&&&&&&&[indexPathsToDelete&addObject:[NSIndexPath&indexPathForRow:i& &&&&&&&&&&&&&&inSection:sectionClosed]]; &&&&&&&&&} &&&&&&&&&[self.mTableView&deleteRowsAtIndexPaths:indexPathsToDelete& &&&&&&&&&&withRowAnimation:UITableViewRowAnimationTop]; &&&&&} &&&&&self.openSectionIndex&=&NSNotFound; &} &-(NSString&*)tableView:(UITableView&*)tableView& &&&&&titleForHeaderInSection:(NSInteger)section &{ &&&&&return&[self.arrayOfSectionHeaders&objectAtIndex:section]; &} &&&&#pragma&UITableView&delegates &-&(NSInteger)numberOfSectionsInTableView:(UITableView&*)tableView&{ &&&&&return&[self.arrayOfSectionHeaders&count]; &} &&&-&(NSInteger)tableView:(UITableView&*)tableView& &&&&numberOfRowsInSection:(NSInteger)section&{ &&&&&&&YDSectionHeaderView&*sectionHeaderView&=& &&&&&&&&&[_arrayOfSectionHeaders&objectAtIndex:section]; &&&&&&&NSInteger&numStoriesInSection&=&sectionHeaderView.numberOfR &&&&&return&sectionHeaderView.expanded&?&numStoriesInSection&:&0; &} &-&(UITableViewCell&*)tableView:(UITableView&*)tableView& &&&&&cellForRowAtIndexPath:(NSIndexPath&*)indexPath&{ &&&&&static&NSString&*CellIdentifier&=&@&Cell&; &&&&&UITableViewCell&*cell&=&(UITableViewCell&*)[tableView& &&&&&&&&&&&dequeueReusableCellWithIdentifier:CellIdentifier]; &&&&&if&(cell&==&nil)&{ &&&&&&&&&cell&=&[[UITableViewCell&alloc]&initWithStyle:UITableViewCellStyleDefault& &&&&&&&&&&&reuseIdentifier:CellIdentifier]; &&&&&} &&&&&cell.selectionStyle&=&UITableViewCellSelectionStyleNone; &&&&&YDSectionHeaderView&*shv=&(YDSectionHeaderView&*)[self.arrayOfSectionHeaders& &&&&&&&&&&&objectAtIndex:indexPath.section]; &&&&&YDCar&*car&=&(YDCar&*)[[self.dataDict&objectForKey:shv.key]& &&&&&&&&&&objectAtIndex:indexPath.row]; &&&&&UILabel*&carlbl&=&[[UILabel&alloc]&initWithFrame:CGRectMake(70,0,250,60)]; &&&&&carlbl.textColor=[UIColor&blueColor]; &&&&&carlbl.backgroundColor=[UIColor&whiteColor]; &&&&&carlbl.text&=&[NSString&stringWithFormat:@&%@&%@&,car.make,car.model]; &&&&&[cell.contentView&addSubview:carlbl]; &&&&&UIImageView&*imgView=[[UIImageView&alloc]&initWithFrame:CGRectMake(0,0,60,60)]; &&&&&imgView.backgroundColor=[UIColor&clearColor]; &&&&&[imgView&setImage:[UIImage&imageNamed:car.imageName]]; &&&&&imgView.contentMode=UIViewContentModeScaleAspectFit; &&&&&[cell.contentView&addSubview:imgView]; &&&&& &&&&&return& &} &-(void)tableView:(UITableView&*)tableView&didSelectRowAtIndexPath: &&&&&&&&(NSIndexPath&*)indexPath &{ &&&&&[tableView&deselectRowAtIndexPath:indexPath&animated:YES]; &} &&&-&(void)didReceiveMemoryWarning &{ &&&&&[super&didReceiveMemoryWarning]; &&&&&&&&&//&Dispose&of&any&resources&that&can&be&recreated. &} &&@end&
喜欢的朋友可以添加我们的微信账号:
51CTO读书频道二维码
51CTO读书频道活动讨论群:【责任编辑: TEL:(010)】&&&&&&
关于&&&&的更多文章
向下一代iOS应用开发前进,你准备好了吗
伴随着应用的下载量超
本书描述了黑客用默默无闻的行动为数字世界照亮了一条道路的故事。
讲师: 35人学习过讲师: 0人学习过讲师: 188人学习过
互联网作为人类文明的全新成果,对中国的意义,首先是
《中国互联网20年:网络产业篇(全彩)》将中国互联网
《中国互联网20年:网络安全篇(全彩)》回顾了我国接
本书将介绍如何创建可交互的Web站点,包括从最简单的订单表单到复杂的安全电子商务站点。而且,读者还将了解如何使用开放源代码
51CTO旗下网站主题 : [已解决]如何固定UITableView的viewForHeaderInSection
级别: 侠客
可可豆: 552 CB
威望: 552 点
在线时间: 111(时)
发自: Web Page
[已解决]如何固定UITableView的viewForHeaderInSection&&&
对于UITableView,我们可以通过代理方法:
- (UIView *)tableView:(UITableView *)tableView viewForHeaderInSection:(NSInteger)section
来为其某一个section添加一个header view。而当你向上滑动tableview时,会发现这个header view到了屏幕的顶部之后就不走了,而下面的table view还在继续向上滑动。
&
我的问题:
&&&&如何能禁掉header view的这个默认行为:“到顶部后不再跟着table view一起滑出界面,而是留在了最顶端。直到这个section滑出界面。” 我需要header view跟着table view 一起滑出界面。
&
&
级别: 侠客
可可豆: 552 CB
威望: 552 点
在线时间: 111(时)
发自: Web Page
没人回答,那就自问自答:还真没有解决次问题的api可以用。但是,有解决办法。用cell或者section来代替这个header view即可。
级别: 新手上路
可可豆: 0 CB
威望: 0 点
在线时间: 9(时)
发自: Web Page
UITableView 设置 style:UITableViewStyleGrouped 之后 sectionHeader 就不浮动了,但是最顶上的sectionheader不能显示出来。
级别: 新手上路
UID: 376720
可可豆: 33 CB
威望: 12 点
在线时间: 14(时)
发自: Web Page
顺利解决我的问题,section的title不随滚动叠加
关注本帖(如果有新回复会站内信通知您)
3*3+1 正确答案:10
发帖、回帖都会得到可观的积分奖励。
按"Ctrl+Enter"直接提交
关注CocoaChina
关注微信 每日推荐
扫一扫 浏览移动版如何固定UITableView的viewForHeaderInSection_百度知道
如何固定UITableView的viewForHeaderInSection
提问者采纳
如何能禁掉header view的这个默认行为,而是留在了最顶端。而当你向上滑动tableview时。直到这个section滑出界面。我的问题,仅供参考。 转载对于UITableView,祝你愉快:- (UIView *)tableView,而下面的table view还在继续向上滑动:(UITableView *)tableView viewForHeaderInSection。” 我需要header view跟着table view 一起滑出界面:“到顶部后不再跟着table view一起滑出界面,我们可以通过代理方法:(NSInteger)section来为其某一个section添加一个header view,会发现这个header view到了屏幕的顶部之后就不走了,满意请采纳
其他类似问题
等待您来回答
下载知道APP
随时随地咨询
出门在外也不愁}

我要回帖

更多关于 uitableview滑动删除 的文章

更多推荐

版权声明:文章内容来源于网络,版权归原作者所有,如有侵权请点击这里与我们联系,我们将及时删除。

点击添加站长微信