如何点击uitableview展开 的HeaderInSection隐藏或展开自section

30520人阅读
&& & & & & & & & & & & & & & & & & & & & & & & & & & & &&如何让 UITableView 的 headerView跟随 cell一起滚动
&& & & &在我们利用 UITableView 展示我们的内容的时候,我需要在顶部放一个不同于一般的cell的 界面,这个界面比较独特。
&& & & &1。&所以我就把它&作为一个section的 headerView。
&& & & &也就是在函数:
&& & & &- (UIView *)tableView:(UITableView *)tableView viewForHeaderInSection:(NSInteger)section
&& & & &里面返回 这个UIView。
&& & & &但是,由于这个UIView占的空间很大,基本占用整个屏幕的高度,而滚动tableView的时候,只滚动cell的内容,而这个section的
headerView却不跟着滚动。
&& & & &后面,我想出了方法2。
&& & & 2。 &设置 tableView的 style为 &UITableViewStyleGrouped,然后让
&& & & & &- (NSInteger)numberOfSectionsInTableView:(UITableView *)tableView
&& & & 返回1。
&& & & &这样确实可以让 headerView ,在滚动tableView的时候,跟随着cell的内容一起滚动。但是,我发现,下面的cell都被加上了边框,
&& & & &而且cell的水平显示范围变窄了。
&& & & &所以尝试了方法3。
&& & & 3。 &将UIView设置为 &整个tableView的headerView,而不是 section 0的headerView
&& & & & & & &self.tableView.tableHeaderView=
&& & & & & & 这样,就可以完美的满足 headerView跟随cell的内容一起滚动的要求拉。
&& & 结论:设置 UIView为 &tableView的tableHeaderView即可实现 headerView跟随tableView一起滚动的效果。
版权声明:本文为博主原创文章,未经博主允许不得转载。
* 以上用户言论只代表其个人观点,不代表CSDN网站的观点或立场
访问:959426次
积分:11543
积分:11543
排名:第550名
原创:244篇
转载:107篇
评论:139条
(1)(15)(9)(1)(3)(4)(6)(1)(2)(2)(2)(2)(2)(2)(4)(2)(8)(16)(10)(23)(14)(7)(16)(4)(3)(3)(15)(3)(42)(6)(17)(16)(1)(4)(7)(6)(9)(17)(1)(1)(5)(2)(6)(1)(3)(1)(9)(7)(2)(1)(4)(5)(7)自定义Section:修改UITableView的Section的背景和字体
第一次尝试修改UITableView的Section的背景和字体,头疼好一阵,终于找到了方法:
如同自定义Cell一样,使用UITableView的函数,可以自定义Section:
- (UIView *)tableView:(UITableView *)tableView
viewForHeaderInSection:(NSInteger)
例子代码:
- (UIView *)tableView:(UITableView *)tableView
viewForHeaderInSection:(NSInteger)section {
&&& NSString
*sectionTitle=[self tableView:tableView
titleForHeaderInSection:section];
(sectionTitle==nil) {
&&& // Create
label with section title
&&& UILabel
*label=[[[UILabel alloc] init] autorelease];
label.frame=CGRectMake(12, 0, 300, 22);
label.backgroundColor=[UIColor clearColor];
label.textColor=[UIColor MujiLightTextColor];
label.font=[UIFont fontWithName:@"Helvetica-Bold"
label.text=sectionT
&&& // Create
header view and add label as a subview
&& &UIView
*sectionView=[[[UIView alloc] initWithFrame:CGRectMake(0, 0,
tableView.bounds.size.width, 22)] autorelease];
&& &[sectionView
setBackgroundColor:[UIColor blackColor]];
&& &[sectionView
addSubview:label];
&& &return
已投稿到:
以上网友发言只代表其个人观点,不代表新浪网的观点或立场。您所在的位置: &
iOS开发UITableView基本使用方法总结
iOS开发UITableView基本使用方法总结
本文为大家呈现了iOS开发中UITableView基本使用方法总结。首先,Controller需要实现两个delegate ,分别是UITableViewDelegate 和UITableViewDataSource;然后 UITableView对象的 delegate要设置为 self;然后就可以实现这些delegate的一些方法拉。
UITableView基本使用方法
1.首先,Controller需要实现两个delegate ,分别是UITableViewDelegate
和UITableViewDataSource
2.然后 UITableView对象的 delegate要设置为
3.然后就可以实现这些delegate的一些方法拉。
(NSInteger)numberOfSectionsInTableView:(UITableView *)tableV
这个方法返回 tableview 有多少个section
&-&(NSInteger)numberOfSectionsInTableView:(UITableView&*)tableView&&{&&return&1;&&}&&
(NSInteger)tableView:(UITableView *)table
numberOfRowsInSection:(NSInteger)
这个方法返回&
&对应的section有多少个元素,也就是多少行。
-&(NSInteger)tableView:(UITableView&*)tableView&numberOfRowsInSection:(NSInteger)section&&{&&return&10;&&}&&
(3)- (CGFloat)tableView:(UITableView
*)tableView heightForRowAtIndexPath:(NSIndexPath*)indexP
这个方法返回指定的 row
(CGFloat)tableView:(UITableView *)tableView
heightForHeaderInSection:(NSInteger)
这个方法返回指定的
section的header view 的高度。
(CGFloat)tableView:(UITableView *)tableView
heightForFooterInSection:(NSInteger)
这个方法返回指定的
section的footer view 的高度。
-&(UITableViewCell&*)tableView:(UITableView&*)tableView&cellForRowAtIndexPath:(NSIndexPath&*)indexPath&&{&&static&NSString&*&showUserInfoCellIdentifier&=&@&ShowUserInfoCell&;&&UITableViewCell&*&cell&=&[tableView_&dequeueReusableCellWithIdentifier:showUserInfoCellIdentifier];&&if&(cell&==&nil)&&{&&&cell&=&[[[UITableViewCell&alloc]&initWithStyle:UITableViewCellStyleSubtitle&&reuseIdentifier:showUserInfoCellIdentifier]&&autorelease];&&}&&&&cell.textLabel.text=@&签名&;&&cell.detailTextLabel.text&=&[NSString&stringWithCString:userInfo.user_signature.c_str()&encoding:NSUTF8StringEncoding];&&}&&
(5)- (CGFloat)tableView:(UITableView
*)tableView heightForHeaderInSection:(NSInteger)section
返回指定的 section
的header的高度
-&(CGFloat)tableView:(UITableView&*)tableView&heightForHeaderInSection:(NSInteger)section&&{&&if&(section&==0)&&return&80.0f;&&else&&return&30.0f;&&}&&
(6)- (NSString
*)tableView:(UITableView *)tableView
titleForHeaderInSection:(NSInteger)section
返回指定的section 的
header&&的 title,如果这个section header&&有返回view,那么title就不起作用了。
-&(NSString&*)tableView:(UITableView&*)tableView&titleForHeaderInSection:(NSInteger)section&&{&&if&(tableView&==&tableView_)&&{&&if&(section&==&0)&&{&&return&@&title&1&;&&}&&else&if&(section&==&1)&&{&&return&@&title&2&;&&}&&else&&{&&return&&&}&&}&&else&&{&&return&&&}&&}&&
(7) - (UIView
*)tableView:(UITableView *)tableView
viewForHeaderInSection:(NSInteger)section
返回指定的 section
header 的view,如果没有,这个函数可以不返回view
-&(UIView&*)tableView:(UITableView&*)tableView&viewForHeaderInSection:(NSInteger)section&&{&&if&(section&==&0)&&{&&&UIView*&header&=&[[[NSBundle&mainBundle]&loadNibNamed:&@&SettingHeaderView&&&owner:&self&&options:&nil]&lastObject];&&&else&&{&&return&&&}&&}&&
(8)&&- (void)tableView:(UITableView
*)tableView didSelectRowAtIndexPath:(NSIndexPath
*)indexPath
当用户选中某个行的cell的时候,回调用这个。但是首先,必须设置tableview的一个属性为可以select 才行。
TableView.allowsSelection=YES;&
cell.selectionStyle=UITableViewCellSelectionStyleB&&
如果不希望响应select,那么就可以用下面的代码设置属性:
TableView.allowsSelection=NO;&&
下面是响应select 点击函数,根据哪个section,哪个row 自己做出响应就好啦。
-&(void)tableView:(UITableView&*)tableView&didSelectRowAtIndexPath:(NSIndexPath&*)indexPath&&{&&if&(indexPath.section&==&1)&&{&&return;&&}&&else&if(indexPath.section==0)&&{&&switch&(indexPath.row)&&{&&&case&0:&&{&&[self&onTalkToFriendBtn];&&}&&break;&&&default:&&break;&&}&&}&&else&&{&&return&;&&}&&&}&&
如何让cell 能够响应 select,但是选中后的颜色又不发生改变呢,那么就设置
cell.selectionStyle =
UITableViewCellSelectionStyleN
-&(UITableViewCell&*)tableView:(UITableView&*)tableView&cellForRowAtIndexPath:(NSIndexPath&*)indexPath&&{&&&cell.selectionStyle&=&UITableViewCellSelectionStyleN&&}&&
(9)如何设置tableview&&每行之间的分割线
self.tableView.separatorStyle=UITableViewCellSeparatorStyleSingleL&&
如果不需要分割线,那么就设置属性为
UITableViewCellSeparatorStyleNone&&即可。
(10)如何设置 tableview cell的背景颜色
-&(UITableViewCell&*)tableView:(UITableView&*)tableView&cellForRowAtIndexPath:(NSIndexPath&*)indexPath&&{&&&cell.contentView.backgroundColor=[UIColor&colorWithRed:0.957&green:0.957&blue:0.957&alpha:1];&&}&&
(11) - (void)tableView:(UITableView *)tableView
accessoryButtonTappedForRowWithIndexPath:(NSIndexPath *)indexPath
这个函数响应,用户点击cell 右边的 箭头(如果有的话)
(12)如何设置tableview 可以被编辑
首先要进入编辑模式:
[TableView&setEditing:YES&animated:YES];&&
如果要退出编辑模式,肯定就是设置为NO
(UITableViewCellEditingStyle)tableView:(UITableView *)tableView
editingStyleForRowAtIndexPath:(NSIndexPath *)indexPath
返回当前cell&&要执行的是哪种编辑,下面的代码是返回删除模式
-&(UITableViewCellEditingStyle)tableView:(UITableView&*)tableView&editingStyleForRowAtIndexPath:(NSIndexPath&*)indexPath&&{&&return&UITableViewCellEditingStyleD&&}&&
-(void) tableView:(UITableView *)aTableView
commitEditingStyle:(UITableViewCellEditingStyle)
editingStyle
forRowAtIndexPath:(NSIndexPath *)indexPath
通知告诉用户编辑了
哪个cell,对应上面的代码,我们在这个函数里面执行删除cell的操作。
-(void)&tableView:(UITableView&*)aTableView&&commitEditingStyle:(UITableViewCellEditingStyle)&editingStyle&&forRowAtIndexPath:(NSIndexPath&*)indexPath&&{&&[chatArray&removeObjectAtIndex:indexPath.row];&&[chatTableView&reloadData];&&}&&
(13)如何获得 某一行的CELL对象
- (UITableViewCell
*)cellForRowAtIndexPath:(NSIndexPath
*)indexP【编辑推荐】【责任编辑: TEL:(010)】
关于&&的更多文章
Android 4.4即将发布,这对大家来说都是很期待的,当然,无论是
既然强大的Android Studio来了,有什么理由不去用呢?
微软和诺基亚终于达成协议,微软将收购诺基亚设备与服
随着秋的到来,各路才子佳人渐渐开始回到学校上课。不
百度推出轻应用引起业界火热议论,收购91和推出轻应用
一个网站,无论视觉上多美观或者内容多丰富,如果它不能适应各种浏览情况并能面向尽可能广泛的用户群,那它就不算是真正成功的网
Windows Phone专家
Android开发专家
51CTO旗下网站收藏,9.4k 浏览
问题对人有帮助,内容完整,我也想知道答案
问题没有实际价值,缺少关键内容,没有改进余地
小弟我初学IOS,想实现这种效果:
我知道整个是使用tableview的group模式实现的,内科和外科都是section,实现成这样的好处是往下拉的时候,sectionName始终位于顶端。
我用原生的方式实现了差不多的效果,但是想实现成图中的效果,还不知道如何着手?
主要问题在于:
section的背景如何自定义?
如何自定义section之间的空白距离?
希望大神给出提示!或是有这种第三方框架?
答案对人有帮助,有参考价值
答案没帮助,是错误的答案,答非所问
- (UIView *)tableView:(UITableView *)tableView viewForHeaderInSection:(NSInteger)section
- (CGFloat)tableView:(UITableView *)tableView heightForHeaderInSection:(NSInteger)section
- (CGFloat)tableView:(UITableView *)tableView heightForRowAtIndexPath:(NSIndexPath *)indexPath
答案对人有帮助,有参考价值
答案没帮助,是错误的答案,答非所问
你说的间距应该是包含在section header 里了,正如楼上所说
不过为了统一iOS6和ios7体验 现在都不用group的方式了 用 plain也完全能达到 有方法可以不让header悬浮
同步到新浪微博
分享到微博?
与我们一起探索更多的未知
专业的开发者技术社区,为用户提供多样化的线上知识交流,丰富的线下活动及给力的工作机会
加入只需一步
关闭理由:
删除理由:
忽略理由:
推广(招聘、广告、SEO 等)方面的内容
与已有问题重复(请编辑该提问指向已有相同问题)
答非所问,不符合答题要求
宜作评论而非答案
带有人身攻击、辱骂、仇恨等违反条款的内容
无法获得确切结果的问题
非开发直接相关的问题
非技术提问的讨论型问题
其他原因(请补充说明)
举报理由:
推广(招聘、广告、SEO 等)方面的内容
带有人身攻击、辱骂、仇恨等违反条款的内容
与已有问题重复(请编辑该提问指向已有相同问题)
不友善内容
答非所问,不符合答题要求
其他原因(请补充说明)
补充说明:
扫扫下载 App
SegmentFault
一起探索更多未知ios8 如果UITableView只设置viewForHeaderInSection,则可能section不能显示,iOS7及以下版本显示正常。解决方案:设置heightForHeaderInSection。- (CGFloat)tableView:(UITableView *)tableView heightForHeaderInSection:(NSInteger)section{
return 22;//自定义高度}&另外,如果代码中设置了titleForHeaderInSection,则不需要上面的设置也可以正常显示。- (NSString *)tableView:(UITableView *)tableView titleForHeaderInSection:(NSInteger) &

我要回帖

更多关于 uitableview点击展开 的文章

 

随机推荐