iphone - 如何将 titleForHeaderInSection 的文本设置为 AlignmentCenter

标签 iphone ios

我用下面的代码在group tableview title header上设置标题,但是默认文本是AlignmentLeft,如何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);
    }


}

最佳答案

这样试试,

- (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;
}

为页眉设置一些默认高度,

- (CGFloat)tableView:(UITableView *)tableViewheightForHeaderInSection:(NSInteger)section {
     switch(section) {
        case 0:
        case 1:
        default:return 20;
     }  
}

关于iphone - 如何将 titleForHeaderInSection 的文本设置为 AlignmentCenter,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/15740704/

相关文章:

ios - 从图像生成配色方案

ios - Xcode 7.0.1 (7A1001) 调试器 "Unable to read data"

ios - 如何在 iOS 中手动触发 touchesBegan 值?

iphone - 创建自定义文本绘图 View

objective-c - 在 ViewController 之间切换时 UIProgressView 开始闪烁

iOS 核心数据 - NSFetchRequest 和 NSPredicate 不允许双重格式?

ios - 如何防止 UITableView 向下滚动?

iphone - 什么是 iOS 4.0 位置 多任务处理能力

iphone - 生产者消费者与核心数据的问题

iphone - NSManagedObject 中的可变集?