Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

how to set text of titleForHeaderInSection to AlignmentCenter

Tags:

ios

iphone

i used the follow code to set the title at the group tableview title header,but default the text is AlignmentLeft,how to AlignmentCenter?

- (NSString *)tableView:(UITableView *)tableView titleForHeaderInSection:(NSInteger)section {
    if(section==0){
        return NSLocalizedString(@"more_titlehead_one", nil);
    }else if (section==1){
        return NSLocalizedString(@"more_titlehead_two", nil);
    }else{
        return NSLocalizedString(@"more_titlehead_three", nil);
    }


}
like image 266
pengwang Avatar asked Jan 17 '26 07:01

pengwang


1 Answers

Try like this,

- (UIView *)tableView:(UITableView *)tableView viewForHeaderInSection:(NSInteger)section {
     UILabel * sectionHeader = [[[UILabel alloc] initWithFrame:CGRectZero] autorelease];
     sectionHeader.backgroundColor = [UIColor clearColor];
     sectionHeader.textAlignment = UITextAlignmentCenter;
     sectionHeader.font = [UIFont boldSystemFontOfSize:10];
     sectionHeader.textColor = [UIColor whiteColor];

     switch(section) {
        case 0:sectionHeader.text = @"TITLE ONE"; break;
        case 1:sectionHeader.text = @"TITLE TWO"; break;
        default:sectionHeader.text = @"TITLE OTHER"; break;
     }  
   return sectionHeader;
}

set some default height for Header,

- (CGFloat)tableView:(UITableView *)tableViewheightForHeaderInSection:(NSInteger)section {
     switch(section) {
        case 0:
        case 1:
        default:return 20;
     }  
}
like image 63
Vedchi Avatar answered Jan 20 '26 00:01

Vedchi



Donate For Us

If you love us? You can donate to us via Paypal or buy me a coffee so we can maintain and grow! Thank you!