为了便于区分各个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;
}
效果如下