IOS 构造和使用TableView(基于storyboard) (五)构建页眉和页脚

为了便于区分各个Section,将Table View的Style属性设置为Grouped


显示一个字符串的页脚页眉

设置页眉

- (NSString *)tableView:(UITableView *)tableView titleForHeaderInSection:(NSInteger)section
{
    NSString *result = nil;
    if ([tableView isEqual:self.myTable] && section == 0) {
        result = @"Section 0 Header";
    }
    return result;
}

设置页脚

- (NSString *)tableView:(UITableView *)tableView titleForFooterInSection:(NSInteger)section
{
    NSString *result = nil;
    if ([tableView isEqual:self.myTable] && section == 0) {
        result = @"Section 0 Footer";
    }
    return result;
}
效果如下


自定义页眉页脚

设置页眉

- (UIView *)tableView:(UITableView *)tableView viewForHeaderInSection:(NSInteger)section
{
    UIView *result = nil;
    if ([tableView isEqual:self.myTable] && section == 0) {
        UILabel *label = [[UILabel alloc]initWithFrame:CGRectZero];
        label.text = @"Section 0 Header";
        label.backgroundColor = [UIColor clearColor];
        [label sizeToFit];
        
        label.frame = CGRectMake(label.frame.origin.x + 10.0f, label.frame.origin.y + 5.0f, label.frame.size.width, label.frame.size.height);
        
        CGRect resultFrame = CGRectMake(0.0f, 0.0f, label.frame.size.width + 10.0f, label.frame.size.height);
        result = [[UIView alloc]initWithFrame:resultFrame];
        [result addSubview:label];
    }
    return result;
}

设置页脚

- (UIView *)tableView:(UITableView *)tableView viewForFooterInSection:(NSInteger)section
{
    UIView *result = nil;
    if ([tableView isEqual:self.myTable] && section == 0) {
        UILabel *label = [[UILabel alloc]initWithFrame:CGRectZero];
        label.text = @"Section 0 Footer";
        label.backgroundColor = [UIColor clearColor];
        [label sizeToFit];
        
        label.frame = CGRectMake(label.frame.origin.x + 10.0f, label.frame.origin.y + 5.0f, label.frame.size.width, label.frame.size.height);
        
        CGRect resultFrame = CGRectMake(0.0f, 0.0f, label.frame.size.width + 10.0f, label.frame.size.height);
        result = [[UIView alloc]initWithFrame:resultFrame];
        [result addSubview:label];
    }
    return result;
}
效果如下


评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值