在tableview的datasource區段加入下面方法
- (NSString *)tableView:(UITableView *)tableView titleForHeaderInSection:(NSInteger)section
{
NSString *sectionName;
switch (section)
{
case 0:
sectionName = NSLocalizedString(@"mySectionName", @"mySectionName");
break;
case 1:
sectionName = NSLocalizedString(@"myOtherSectionName", @"myOtherSectionName");
break;
// ...
default:
sectionName = @"";
break;
}
return sectionName;
}
另一種可以插入Label
- (UIView *)tableView:(UITableView *)tableView viewForHeaderInSection:(NSInteger)section {
//UIImage *myImage = [UIImage imageNamed:@"BUTTON.png"];
NSString *sectionName = nil;
switch (section) {
case 0:
sectionName = [NSString stringWithFormat:@"Header Text 1"];
break;
case 1:
sectionName = [NSString stringWithFormat:@"Header Text 2"];
break;
case 2:
sectionName = [NSString stringWithFormat:@"Header Text 3"];
break;
}
UILabel *sectionHeader = [[[UILabel alloc] initWithFrame:CGRectMake(0, 0, 200, 40)] autorelease];
sectionHeader.backgroundColor = [UIColor clearColor];
sectionHeader.font = [UIFont boldSystemFontOfSize:18];
sectionHeader.textColor = [UIColor whiteColor];
sectionHeader.text = sectionName;
return sectionHeader;
}
如果上第二種方法,那要加入下面方法來控制section header的高度
- (CGFloat)tableView:(UITableView *)tableView heightForHeaderInSection:(NSInteger)section
{
if (section ==0)
return 80.0f;
else
return 30.0f;
}
That's because the UITableView automatically sets the frame of the header view you provide to
(0, y, table view width, header view height)
y is the computed position of the view and header view height is the value returned by tableView:heightForHeaderInSection:
沒有留言:
張貼留言