UITableView headerViewForSection 返回 (空值)
2015-10-14 15:41
447 查看
我有
我已经创建了一个自定义
后来,我计划有点修改它,所以我需要使用
作为一个简单的例子,我在试着
示例代码:
需要创建自定义
好的在一些试验和错误后我终于解决我自己的困境。
我做了
为一个单元格,我们会采取
虽然......
为的页眉页脚,我们将采取
剩下的就很多相同的概念作为一个单元格。
设置的页眉的高度到 40
设置的节数为 2 或更多
每节要至少 1 个设置的行数
iOS6 + (
创建和使用默认的
使用自定义
创建
创建了一个视图界面 nib 文件的类
Xib,在选定视图 & 在中它是身份检查器
指定的自定义类
作的性质、 合成和连接他们的 xib
修改
PS: 与问题
使用一个简单的
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) { case 0: [myHeader setBackgroundColor:[UIColor greenColor]]; break; case 1: [myHeader setBackgroundColor:[UIColor redColor]]; break; default: break; } 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 myHeader; } - (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:
方法 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 myHeader; } - (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 *lblSomething;
@property (strong, nonatomic) IBOutlet UIButton *btnSomething;
修改
-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 myHeader; } - (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,然后看看下一个方法
方法 2:
使用一个简单的 UIView:
-(UIView *)tableView:(UITableView *)tableView viewForHeaderInSection:(NSInteger)section { UIView *vwHeader = [[UIView alloc] init]; [vwHeader setTag:200 + section]; //[1] first method switch (section) { case 0: [vwHeader setBackgroundColor:[UIColor greenColor]]; break; case 1: [vwHeader setBackgroundColor:[UIColor redColor]]; break; default: break; } 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 vwHeader; } - (void)tableView:(UITableView *)tableView didSelectRowAtIndexPath:(NSIndexPath *)indexPath { UIView *vwTest = [self.tableView viewWithTag:200 + indexPath.section]; //[1] NSLog(@"[1] : %@",vwTest); //or UILabel *lblTest = (UILabel *)[self.tableView viewWithTag:100 + indexPath.section]; //[2] NSLog(@"%@",lblTest.text); UIView *vwTestForSuperview = lblTest.superview; NSLog(@"[2] : %@",vwTestForSuperview); }
相关文章推荐
- UITapGestureRecognizer 和UIPanGestureRecognizer的使用,触摸和滑动
- UITableView的代理方法不执行的解决方案
- UIActivityIndicatorView的详细使用
- 【Android学习笔记】 蓝牙Bluetooth
- 对于动画UIDynamicAnimator的学习
- siliverlight windowless=true 模式下禁止输入中文
- easyui datagrid组件应用
- 10、建造者模式(Builder)
- 【Android】Field requires API level 4 (current min is 1): android.os.Build.VERSION#SDK_INT
- CAShapeLayer 结合 UIBezierPath 画圆
- Zynq Reseving Physical Memory Issue
- TAILQ_QUEUE队列
- 用gitUI 提交 github
- jQuey中的$(document).read(function(){})与js中window.onload的区别?
- 错误The request sent by the client was syntactically incorrect ()的解决
- iOS开发UI篇—CALayer简介
- UIButton的UIEdgeInsets功能:设置button上的文字和图片的位置
- Error —— 打出log:finished with non-zero exit value 2
- puer实现前后端分离开发
- Android 数据查询query函数参数解析