方法一:
好的,经过反复试验,我终于解决了自己的困境。
我做了headerView,就像我做一个单元格一样。
对于单元格,我们将使用 UITableViewCell 并使用 dequeueReusableCellWithIdentifier
同时...
对于页眉/页脚,我们将采用UITableViewHeaderFooterView 并使用dequeueReusableHeaderFooterViewWithIdentifier 方法。
其余部分与单元格的概念几乎相同。
先决条件:
- 将标题高度设置为 40
- 将节数设置为 2 个或更多
- 将每个部分的行数设置为至少 1
- iOS6+(
UITableViewHeaderFooterView 不适用于 iOS5 及更低版本)
第一种方法:
在-viewForHeaderInSection: 方法中创建和使用默认UITableViewHeaderFooterView:
- (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
- 为该类创建了一个 View 界面 nib 文件
- 在 xib 中,选择 View & 在它的 Identity Inspector
- 制作属性,在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,那么请参阅下一个方法
方法二:
使用简单的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);
}
PS:我知道这段代码没有什么伟大的用途,但这只是其他人的一个例子。