怎么为tableview headerview添加header

UITableView使用详解
在开发iphone的应用时基本上都要用到UITableView,这里讲解一下UITableView的使用方法及代理的调用情况
- (void)viewDidLoad
viewDidLoad];
//初始化数据
NSArray *array1_=@[@"张铁林",@"张国立",@"张国荣",@"张艺谋",@"张惠妹"];
*array2_=@[@"李小龙",@"李小路"];
*array3_=@[@"王刚"];
& self.myDic=@{@"老张家":array1_,
@"老李家":array2_,
@"老王家":array3_};
UITableView *myTableView_=[[UITableView alloc] initWithFrame:CGRectMake(0,
460) style:UITableViewStylePlain];
& myTableView_.delegate=self;
& myTableView_.dataSource=self;
//改变换行线颜色</
& myTableView_.separatorColor = [UIColor blueColor];
//设定Header的高度,
& myTableView_.sectionHeaderHeight=50;
//设定footer的高度,
& myTableView_.sectionFooterHeight=100;
//设定行高
& myTableView_.rowHeight=100;
//设定cell分行线的样式,默认为UITableViewCellSeparatorStyleSingleLine
[myTableView_ setSeparatorStyle:UITableViewCellSeparatorStyleSingleLine];
//设定cell分行线颜色
[myTableView_ setSeparatorColor:[UIColor redColor]];
//编辑tableView
& myTableView_.editing=NO;
& [self.view addSubview:myTableView_];
//跳到指的row
or section
[myTableView_ scrollToRowAtIndexPath:[NSIndexPath indexPathForRow:2
inSection:2]
atScrollPosition:UITableViewScrollPositionBottom
animated:NO];
//指定有多少个分区(Section),默认为1
- (NSInteger)numberOfSectionsInTableView:(UITableView
*)tableView {
[[self.myDic allKeys] count];
//每个section底部标题高度(实现这个代理方法后前面
sectionHeaderHeight 设定的高度无效)
-(CGFloat)tableView:(UITableView *)tableView
heightForHeaderInSection:(NSInteger)section{
//每个section头部标题高度(实现这个代理方法后前面
sectionFooterHeight 设定的高度无效)
-(CGFloat)tableView:(UITableView *)tableView
heightForFooterInSection:(NSInteger)section{
//每个section头部的标题-Header
- (NSString *)tableView:(UITableView *)tableView
titleForHeaderInSection:(NSInteger)section{
[[self.myDic allKeys] objectAtIndex:section];
//每个section底部的标题-Footer
- (NSString *)tableView:(UITableView *)tableView
titleForFooterInSection:(NSInteger)section{
return nil;
//用以定制自定义的section头部视图-Header
-(UIView *)tableView:(UITableView *)tableView
viewForHeaderInSection:(NSInteger)section{
return nil;
//用以定制自定义的section底部视图-Footer
-(UIView *)tableView:(UITableView *)tableView
viewForFooterInSection:(NSInteger)section{
& UIImageView
*imageView_=[[UIImageView
alloc]initWithFrame:CGRectMake(0, 0,
320, 20)];
& imageView_.image=[UIImage imageNamed:@"1000.png"];
[imageView_ autorelease];
//指定每个分区中有多少行,默认为1
- (NSInteger)tableView:(UITableView *)tableView
numberOfRowsInSection:(NSInteger)section{
return [[self.myDic
objectForKey:[[self.myDic
allKeys]objectAtIndex:section]] count];
//改变行的高度(实现主个代理方法后
设定的高度无效)
- (CGFloat)tableView:(UITableView *)tableView
heightForRowAtIndexPath:(NSIndexPath *)indexPath{
//绘制Cell
-(UITableViewCell *)tableView:(UITableView *)tableView
cellForRowAtIndexPath:(NSIndexPath *)indexPath {
NSString *SimpleTableIdentifier
= @"SimpleTableIdentifier";
UITableViewCell *cell = [tableView
dequeueReusableCellWithIdentifier:
& SimpleTableIdentifier];
& if (cell ==
& & cell = [[[UITableViewCell alloc] initWithStyle:UITableViewCellStyleDefault
reuseIdentifier:
SimpleTableIdentifier] autorelease];
& &//设定附加视图
setAccessoryType:UITableViewCellAccessoryDetailDisclosureButton];
UITableViewCellAccessoryNone, & &
& // 没有标示
UITableViewCellAccessoryDisclosureIndicator,&
& // 下一层标示
UITableViewCellAccessoryDetailDisclosureButton, // 详情button
UITableViewCellAccessoryCheckmark &
& &//设定选中cell时的cell的背影颜色
&&cell.selectionStyle=UITableViewCellSelectionStyleBlue;
& & //选中时蓝色效果
cell.selectionStyle=UITableViewCellSelectionStyleN
//选中时没有颜色效果
cell.selectionStyle=UITableViewCellSelectionStyleG
&//选中时灰色效果
& & //自定义选中cell时的背景颜色
& & UIView *selectedView =
[[UIView alloc] initWithFrame:cell.contentView.frame];
& & selectedView.backgroundColor
= [UIColor orangeColor];
& & cell.selectedBackgroundView =
& & [selectedView
cell.selectionStyle=UITableViewCellAccessoryN //行不能被选中
& //这是设置没选中之前的背景颜色
cell.contentView.backgroundColor = [UIColor clearColor];
cell.imageView.image=[UIImage imageNamed:@"1001.jpg"];//未选cell时的图片
cell.imageView.highlightedImage=[UIImage imageNamed:@"1002.jpg"];//选中cell后的图片
& cell.textLabel.text=[[self.myDic objectForKey:[[self.myDic allKeys]objectAtIndex:indexPath.section]]objectAtIndex:indexPath.row];
-(NSInteger)tableView:(UITableView *)tableView
indentationLevelForRowAtIndexPath:(NSIndexPath *)indexPath{
& NSUInteger
row = [indexPath row];
//选中Cell响应事件
- (void)tableView:(UITableView *)tableView
didSelectRowAtIndexPath:(NSIndexPath *)indexPath{
[tableView deselectRowAtIndexPath:indexPath animated:YES];//选中后的反显颜色即刻消失
//得到当前选中的cell
& UITableViewCell *cell=[tableView
cellForRowAtIndexPath:indexPath];
& NSLog(@"cell=%@",cell);
//行将显示的时候调用,预加载行
-(void)tableView:(UITableView *)tableView
willDisplayCell:(UITableViewCell *)cell
forRowAtIndexPath:(NSIndexPath
*)indexPath
NSLog(@"将要显示的行是\n
cell=%@& \n indexpath=%@",cell,indexPath);
//选中之前执行,判断选中的行(阻止选中第一行)
-(NSIndexPath *)tableView:(UITableView *)tableView
willSelectRowAtIndexPath:(NSIndexPath *)indexPath
& NSUInteger
row = [indexPath row];
& if (row ==
return nil;
//编辑状态,点击删除时调用
- (void)tableView:(UITableView *)tableView
commitEditingStyle:(UITableViewCellEditingStyle)editingStyle
forRowAtIndexPath:(NSIndexPath *)indexPath
//cell右边按钮格式为UITableViewCellAccessoryDetailDisclosureButton时,点击按扭时调用的方法
-(void)tableView:(UITableView *)tableView
accessoryButtonTappedForRowWithIndexPath:(NSIndexPath *)indexPath{
NSLog(@"当前点击的详情button
\n indexpath=%@",indexPath);
//右侧添加一个索引表
- (NSArray
*)sectionIndexTitlesForTableView:(UITableView *)tableView{
[self.myDic allKeys];
//划动cell是否出现del按钮
- (BOOL)tableView:(UITableView *)tableView
canEditRowAtIndexPath:(NSIndexPath *)indexPath {
return YES;
//设定横向滑动时是否出现删除按扭,(阻止第一行出现)
-(UITableViewCellEditingStyle)tableView:(UITableView
*)tableView editingStyleForRowAtIndexPath:(NSIndexPath *)indexPath
(indexPath.row==0) {
& & return
UITableViewCellEditingStyleNone;
& & return
UITableViewCellEditingStyleDelete;
UITableViewCellEditingStyleDelete;
//自定义划动时delete按钮内容
- (NSString *)tableView:(UITableView *)tableView
titleForDeleteConfirmationButtonForRowAtIndexPath:(NSIndexPath
*)indexPath{
return @"删除这行";
//开始移动row时执行
-(void)tableView:(UITableView *)tableView
moveRowAtIndexPath:(NSIndexPath
*)sourceIndexPath toIndexPath:(NSIndexPath*)destinationIndexPath
& NSLog(@"sourceIndexPath=%@",sourceIndexPath);
& NSLog(@"sourceIndexPath=%@",destinationIndexPath);
//滑动可以编辑时执行
-(void)tableView:(UITableView *)tableView
willBeginEditingRowAtIndexPath:(NSIndexPath *)indexPath
NSLog(@"willBeginEditingRowAtIndexPath");
//将取消选中时执行,
也就是上次先中的行
-(NSIndexPath *)tableView:(UITableView *)tableView
willDeselectRowAtIndexPath:(NSIndexPath *)indexPath
NSLog(@"上次选中的行是&
\n indexpath=%@",indexPath);
//让行可以移动
-(BOOL)tableView:(UITableView *)tableView
canMoveRowAtIndexPath:(NSIndexPath *)indexPath
return NO;
//移动row时执行
-(NSIndexPath *)tableView:(UITableView *)tableView
targetIndexPathForMoveFromRowAtIndexPath:(NSIndexPath *)sourceIndexPath
toProposedIndexPath:(NSIndexPath
*)proposedDestinationIndexPath
NSLog(@"targetIndexPathForMoveFromRowAtIndexPath");
& //用于限制只在当前section下面才可以移动
& if(sourceIndexPath.section !=
proposedDestinationIndexPath.section){
sourceIndexP
proposedDestinationIndexP
已投稿到:
以上网友发言只代表其个人观点,不代表新浪网的观点或立场。当前位置: >
> UITableView SDK嘱托方法详解
UITableView SDK嘱托方法详解
xiangxing008 & at
UITableView SDK委托方法详解  今天跟大家分享一下UITableView的各个代理方法的用法,主要是根据SDK里面的介绍,再加上Atany自己的分析与测试,总结成此文。如果有疑问的话,希望大家留言告诉我~
  本文主要讲解对于UITableView最重要的两个协议
  UITableViewDataSource
  UITableViewDelegate
  UITableViewDataSource
  1)tableView:cellForRowAtIndexPath:
  申请一个cell插入到表视图特定的位置,cell生成访问到的方法。
  2)numberOfSectionsInTableView:
  返回表视图的分区数。
  3)tableView:numberOfRowsInSection:
  返回表视图中每个分区的行数。
  4)sectionIndexTitlesForTableView:
  返回分区索引的名称,使用此方法会在表视图右侧创建一个索引栏,通过点击索引可以快速跳转到指定分区。
  5)tableView:sectionForSectionIndexTitle:atIndex:
  点击索引栏会调用此事件,通过点击的标题与索引返回分区的索引。简单来说,就是设定点击右侧索引栏会跳转到的分区,如return 0,那么无论点击什么,都会跳转到分区0。
  6)tableView:titleForHeaderInSection:
  定义每一个分区头的名称。
  7)tableView:titleForFooterInSection:
  定义每一个分区尾的名称。
  8)tableView:commitEditingStyle:forRowAtIndexPath:
  要求数据源提交插入或者删除指定行的事件。即每次删除或者插入完成后都会响应该方法,commitEditingStyle参数标识此次操作是UITableViewCellEditingStyleInsert(插入)
UITableViewCellEditingStyleDelete(删除)。
  9)tableView:canEditRowAtIndexPath:
  使得指定行为可编辑状态。
  10)tableView:canMoveRowAtIndexPath:
  使得指定行成可移动状态,如果指定为NO,则该行不会出现可拖动图标。
  11)tableView:moveRowAtIndexPath:toIndexPath:
  移动行之后调用的方法,可以在里面设置表视图数据list的一些操作。
  UITableViewDelegate
  1)tableView:heightForRowAtIndexPath:
  返回行的高度。
  2)tableView:willDisplayCell:forRowAtIndexPath:
  在Cell将要显示时调用此方法,可以在这里修改重用的Cell的一些属性,例如颜色。
  3)tableView:willSelectRowAtIndexPath:
  通知委托指定行被将要选中,返回&#20540;为NSIndexPath是指返回响应行的索引,即可以点击一行而返回另一行索引,如果不想点击事件响应则返回nil。
  4)tableView:didSelectRowAtIndexPath:
  通知委托指定行被选中。
  5)tableView:willDeselectRowAtIndexPath:
  通知委托指定行将取消选中。
  6)tableView:didDeselectRowAtIndexPath:
  通知委托指定行被取消选中。
  关于3)到6)的执行顺序:
  willSelectRowAtIndexPath 当前行为:0
  didSelectRowAtIndexPath 当前行为:0
  willSelectRowAtIndexPath 当前行为:1
  willDeselectRowAtIndexPath 当前行为:0
  didDeselectRowAtIndexPath 当前行为:0
  didSelectRowAtIndexPath 当前行为:1
  注:在下一行将要选中后才取消上一行的选中。
  &7)tableView:viewForHeaderInSection:
  设置分区的header的视图,可以为它添加图片或者其他控件(UIView的子类)
  8)tableView:viewForFooterInSection:
  设置分区footer的视图,可以为它添加图片或者其他控件(UIView的子类)
  9)tableView:heightForHeaderInSection:
  设置分区header的高度。
  10)tableView:heightForFooterInSection:
  设置分区footer的高度
  11)tableView:willDisplayHeaderView:forSection:
  通知委托将要显示分区header的视图
  12)tableView:willDisplayFooterView:forSection:
  通知委托将要显示分区footer的视图
  13)tableView:willBeginEditingRowAtIndexPath:
  通知委托,表视图将要被编辑(删除或者插入,而是不是编辑cell文字哦)
  14)tableView:didEndEditingRowAtIndexPath:
  表视图完成编辑。
  15)tableView:editingStyleForRowAtIndexPath:
  对当前行设置编辑模式,删除、插入或者不可编辑。
  typedef enum {
  //不可编辑
  UITableViewCellEditingStyleNone,
  //删除&&
  UITableViewCellEditingStyleDelete,
  //插入
  UITableViewCellEditingStyleInsert
  }UITableViewCellEditingS
  16)tableView:titleForDeleteConfirmationButtonForRowAtIndexPath:
  在删除模式启动下,改变每行删除按钮的文字(默认为“Delete”)
  17)tableView:shouldIndentWhileEditingRowAtIndexPath:
  通知委托在编辑模式下是否需要对表视图指定行进行缩进,NO为关闭缩进,这个方法可以用来去掉move时row前面的空白。
  18)tableView:targetIndexPathForMoveFromRowAtIndexPath:toProposedIndexPath:
  移动行的过程中会多次调用此方法,返回&#20540;代表进行移动操作后回到的行,如果设置为当前行,则不论怎么移动都会回到当前行。
  19)tableView:didEndDisplayingCell:forRowAtIndexPath:
  通知委托指定的cell已经移出表视图,即拖动的时候移出视图的cell。
  20)tableView:didEndDisplayingHeaderView:forSection:
&通知委托指定的分区头已经移出表视图。
  21)tableView:didEndDisplayingFooterView:forSection:
  通知委托指定的分区尾已经移出视图
  22)tableView:shouldShowMenuForRowAtIndexPath:
  通知委托是否在指定行显示菜单,返回&#20540;为YES时,长按显示菜单。
  23)tableView:canPerformAction:forRowAtIndexPath:withSender:
  弹出选择菜单时会调用此方法(复制、粘贴、全选、剪切)
  24)tableView:performAction:forRowAtIndexPath:withSender:
  选择菜单项完成之后调用此方法。
  25)tableView:shouldHighlightRowAtIndexPath:
  通知委托是否开启点击高亮显示,YES为显示。
  26)tableView:didHighlightRowAtIndexPath:
  通知委托表示图的指定行被高亮显示。
  27)tableView:didUnhighlightRowAtIndexPath:
通知委托表视图的指定行不在高亮显示,一般是点击其他行的时候。
  关于UIViewTable更多的学习:自定义UITableViewCell详解[两种方法]
  博文地址:
  博客地址:
  —— by atany
本文遵循“署名-非商业用途-保持一致”创作公用协议
本问题标题:
本问题地址:
温馨提示:本问题已经关闭,不能解答。
暂无合适的专家
&&&&&&&&&&&&&&&
希赛网 版权所有 & &&&&湘教QS2-164&&增值电信业务经营许可证湘B2-赶快加入吧
收藏,7.7k 浏览
问题对人有帮助,内容完整,我也想知道答案
问题没有实际价值,缺少关键内容,没有改进余地
对于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 一起滑出界面。为了说明,附上一张图:
答案对人有帮助,有参考价值
答案没帮助,是错误的答案,答非所问
实现方法,sectionview=nil。
每一个section的第一行作为sectionview。
答案对人有帮助,有参考价值
答案没帮助,是错误的答案,答非所问
有一个问题向上使劲滑动的时候,不会到达uitableview的顶部
(void)scrollViewDidScroll:(UIScrollView *)scrollView {
if (scrollView == self.myTableView)
//YOUR_HEIGHT
为最高的那个headerView的高度
CGFloat sectionHeaderHeight = YOUR_HEIGHT;
if (scrollView.contentOffset.y&=sectionHeaderHeight&&scrollView.contentOffset.y&=0) {
scrollView.contentInset = UIEdgeInsetsMake(-scrollView.contentOffset.y, 0, 0, 0);
} else if (scrollView.contentOffset.y&=sectionHeaderHeight) {
scrollView.contentInset = UIEdgeInsetsMake(-sectionHeaderHeight, 0, 0, 0);
同步到新浪微博
不要错过该问题的后续解决方案
如果你没找到答案,记得登录关注哦,大家会尽全力帮你解决的 ^___^
最专业的开发者社区
最前沿的技术问答,最纯粹的技术切磋。让你不知不觉中开拓眼界,提高技能,认识更多朋友。
分享到微博?
关闭理由:
删除理由:
忽略理由:
与已有问题重复
答非所问,不符合答题要求
宜作评论而非答案
带有人身攻击、辱骂、仇恨等违反条款的内容
内容质量差,或不适合在本网站出现
无意义讨论型问题
其他原因(请补充说明)
举报理由:
带有人身攻击、辱骂、仇恨等违反条款的内容
与已有问题重复
内容质量差,或不适合在本网站出现
答非所问,不符合答题要求
其他原因(请补充说明)
补充说明:[ios]UITableView headerViewForSection 返回 (空值)
我有 UITableView 与 2 个部分。每个都有它自己的 headerView 。我已经创建了一个自定义 headerView 通过 -viewForHeaderInSection: 方法。后来,我计划有点修改它,所以我需要使用 viewForHeader 方法,但不能访问 headerView 和它有 subViews 。
作为一个简单的例子,我在试着 NSLog viewForHeader 中的对象 -didSelectRowAtIndexPath: 但我得到 (null) 的结果。
示例代码:
-(CGFloat)tableView:(UITableView *)tableView heightForHeaderInSection:(NSInteger)section
return 75;
-(UIView *)tableView:(UITableView *)tableView viewForHeaderInSection:(NSInteger)section
UIView *myHeader = [[UIView alloc] init];
switch (section) {
[myHeader setBackgroundColor:[UIColor greenColor]];
[myHeader setBackgroundColor:[UIColor redColor]];
UILabel *myLabel = [[UILabel alloc] init];
[myLabel setFrame:CGRectMake(10, 0, 100, 30)];
[myLabel setTag:101];
[myLabel setBackgroundColor:[UIColor clearColor]];
[myLabel setText:[NSString stringWithFormat:@"Section: %d",section]];
[myHeader addSubview:myLabel];
return myH
- (void)tableView:(UITableView *)tableView didSelectRowAtIndexPath:(NSIndexPath *)indexPath
UITableViewHeaderFooterView *testView = [self.tableView headerViewForSection:indexPath.section];
NSLog(@"%@",testView);
//displays (null)
需要创建自定义 UIView 作为 xib headerView 吗?(因为每一个类似的问题和文档")
To make the table view aware of your header or footer view, you need to register it.
You do this using the registerNib:forCellReuseIdentifier: or
registerClass:forCellReuseIdentifier: method of UITableView.
解决方法 1:
好的在一些试验和错误后我终于解决我自己的困境。我做了 headerView 只会一样一个单元格。
为一个单元格,我们会采取 UITableViewCell 和使用dequeueReusableCellWithIdentifier虽然......为的页眉页脚,我们将采取 UITableViewHeaderFooterView ,并使用 dequeueReusableHeaderFooterViewWithIdentifier 方法。
剩下的就很多相同的概念作为一个单元格。
先决条件:
设置的页眉的高度到 40
设置的节数为 2 或更多
每节要至少 1 个设置的行数
iOS6 + ( UITableViewHeaderFooterView 与 iOS5 和下面不会工作)
第一种方法:
创建和使用默认的 UITableViewHeaderFooterView 内 -viewForHeaderInSection: 方法:
- (UIView *)tableView:(UITableView *)tableView viewForHeaderInSection:(NSInteger)section
static NSString *HeaderIdentifier = @"header";
UITableViewHeaderFooterView *myHeader = [tableView dequeueReusableHeaderFooterViewWithIdentifier:HeaderIdentifier];
if(!myHeader) {
myHeader = [[UITableViewHeaderFooterView alloc] initWithReuseIdentifier:HeaderIdentifier];
UIButton *btnUp = [UIButton buttonWithType:UIButtonTypeRoundedRect];
[btnUp setTag:101];
[btnUp setTitle:@"-" forState:UIControlStateNormal];
[btnUp setFrame:CGRectMake(tableView.frame.size.width - 35, 5, 30, 30)];
[myHeader addSubview:btnUp];
[myHeader.textLabel setText:[NSString stringWithFormat:@"Section: %d",section]];
[myHeader setFrame:CGRectMake(0, 0, tableView.frame.size.width, 50)];
return myH
- (void)tableView:(UITableView *)tableView didSelectRowAtIndexPath:(NSIndexPath *)indexPath
UITableViewHeaderFooterView *theHeaderView = [tableView headerViewForSection:indexPath.section];
NSLog(@"%@",theHeaderView); // -- great! ... not (null) anymore
UIButton *theButton = (UIButton *)[theHeaderView viewWithTag:101];
[theButton setTitle:@"+" forState:UIControlStateNormal];
第二种方法:
使用自定义 UITableViewHeaderFooterView 子类:
创建 UITableViewHeaderFooterView 子类并将它命名为CustomHeaderView
创建了一个视图界面 nib 文件的类
Xib,在选定视图 & 在中它是身份检查器
指定的自定义类CustomHeaderView
作的性质、 合成和连接他们的 xib
@property (strong, nonatomic) IBOutlet UILabel *lblS
@property (strong, nonatomic) IBOutlet UIButton *btnS
修改 -viewForHeaderInSection: & -didSelectRowAtIndexPath: 作为:
- (UIView *)tableView:(UITableView *)tableView viewForHeaderInSection:(NSInteger)section
static NSString *HeaderIdentifier = @"header";
CustomHeaderView *myHeader = [tableView dequeueReusableHeaderFooterViewWithIdentifier:HeaderIdentifier];
if(!myHeader) {
[tableView registerClass:[CustomHeaderView class] forHeaderFooterViewReuseIdentifier:HeaderIdentifier];
myHeader = [[[NSBundle mainBundle] loadNibNamed:@"CustomHeaderView"
owner:self
options:nil] objectAtIndex:0];
[myHeader.btnSomething setTitle:@"-" forState:UIControlStateNormal];
[myHeader.lblSomething setText:[NSString stringWithFormat:@"Section: %d",section]];
return myH
- (void)tableView:(UITableView *)tableView didSelectRowAtIndexPath:(NSIndexPath *)indexPath
CustomHeaderView *theHeaderView = (CustomHeaderView*)[tableView headerViewForSection:indexPath.section];
NSLog(@"%@",theHeaderView);
[theHeaderView.lblSomething setAlpha:theHeaderView.lblSomething.alpha-0.1];
[theHeaderView.btnSomething setTitle:@"+" forState:UIControlStateNormal];
PS: 与问题 UITableViewHeaderFooterView 是它是 iOS6 + 如果,任何原因,您的标头/必须是 UIView ,然后看看下一个方法
使用一个简单的 UIView :
-(UIView *)tableView:(UITableView *)tableView viewForHeaderInSection:(NSInteger)section
UIView *vwHeader = [[UIView alloc] init];
[vwHeader setTag:200 + section]; //[1] first method
switch (section) {
[vwHeader setBackgroundColor:[UIColor greenColor]];
[vwHeader setBackgroundColor:[UIColor redColor]];
UILabel *lblTitle = [[UILabel alloc] init];
[lblTitle setFrame:CGRectMake(10, 0, 100, 30)];
[lblTitle setTag:100 + section]; //[2] alternative method
[lblTitle setBackgroundColor:[UIColor clearColor]];
[lblTitle setText:[NSString stringWithFormat:@"Section: %d",section]];
[vwHeader addSubview:lblTitle];
return vwH
- (void)tableView:(UITableView *)tableView didSelectRowAtIndexPath:(NSIndexPath *)indexPath
UIView *vwTest = [self.tableView viewWithTag:200 + indexPath.section]; //[1]
NSLog(@"[1] : %@",vwTest);
UILabel *lblTest = (UILabel *)[self.tableView viewWithTag:100 + indexPath.section]; //[2]
NSLog(@"%@",lblTest.text);
UIView *vwTestForSuperview = lblTest.
NSLog(@"[2] : %@",vwTestForSuperview);
PS:我知道此代码不会服务于任何伟大的目的,但这只是示例的其他人。}

我要回帖

更多关于 tableview 的文章

更多推荐

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

点击添加站长微信