如何使UITableView的节标题居中

32 cocoa-touch uitableview ios

我想把标题集中在tableview上,但是我有2个问题.首先我没有正确的高度,其次UILabel看起来不像默认标题 - 字体/字体大小/颜色等...是否有更好的方法来居中它,和/或有没有办法制作它看起来像默认标头.

- (UIView *)tableView:(UITableView *)tableView viewForHeaderInSection:(NSInteger) section
{
    //section text as a label
    UILabel *lbl = [[UILabel alloc] init];
    lbl.textAlignment = UITextAlignmentCenter;  

    lbl.text = @"Header";
    [lbl setBackgroundColor:[UIColor clearColor]];  

    return lbl;
}
Run Code Online (Sandbox Code Playgroud)

Bra*_*all 26

这应该可以解决你的问题.在xcode 6/ios8中测试

- (NSString *)tableView:(UITableView *)tableView titleForHeaderInSection:(NSInteger)section {
    [[UILabel appearanceWhenContainedIn:[UITableViewHeaderFooterView class], nil] setTextAlignment:NSTextAlignmentCenter];
    return [sectionTitles objectAtIndex:section];
}
Run Code Online (Sandbox Code Playgroud)


vda*_*bry 25

您还必须实现tableView:heightForHeaderInSection来调整标题高度:

在UITableViewDelegate协议参考文档中,您会发现:

的tableView:viewForHeaderInSection:

讨论

例如,返回的对象可以是UILabel或UIImageView对象.表视图自动调整节标题的高度以适应返回的视图对象. 只有在实现tableView:heightForHeaderInSection:时,此方法才能正常工作.

要使标题标注居中,必须在将其对齐设置为居中之前指定标签框.要获取标准字体,请使用SystemFontOfSize:

还要注意你正在创建一个内存泄漏,你应该返回一个自动释放的视图

尝试这样的事情:

UILabel *lbl = [[[UILabel alloc] initWithFrame:CGRectMake(0, 0, self.view.frame.size.width, 30)] autorelease];
lbl.textAlignment = UITextAlignmentCenter;
lbl.font = [UIFont systemFontOfSize:12];
Run Code Online (Sandbox Code Playgroud)

希望这有帮助,文森特


Vol*_*ing 19

如果您定位iOS6及更高版本,则只需要将标题标题居中,就不必提供自己的标题视图.

刚刚实施

- tableView:willDisplayHeaderView:forSection:

并设置标签的textAligment属性:

- (void)tableView:(UITableView *)tableView willDisplayHeaderView:(UIView *)view forSection:(NSInteger)section
{
    if([view isKindOfClass:[UITableViewHeaderFooterView class]]){

        UITableViewHeaderFooterView *tableViewHeaderFooterView = (UITableViewHeaderFooterView *) view;
        tableViewHeaderFooterView.textLabel.textAlignment = NSTextAlignmentCenter;
    }
}
Run Code Online (Sandbox Code Playgroud)

  • 这应该是公认的答案.使用[UILabel appearanceWhenContainedIn:UITableViewHeaderFooterView.class ...]技巧将改变*all*你的标题在*all*你的app中的tableviews (4认同)

Tal*_*ham 17

Volker在Swift中的回答:

如果您定位iOS6及更高版本,则只需要将标题标题居中,就不必提供自己的标题视图.

刚刚实施

- tableView:willDisplayHeaderView:forSection:
Run Code Online (Sandbox Code Playgroud)

并设置标签的textAligment属性:

func tableView(tableView: UITableView, willDisplayHeaderView view: UIView, forSection section: Int) {
    if let headerView = view as? UITableViewHeaderFooterView {
        headerView.textLabel?.textAlignment = .Center
    }
}
Run Code Online (Sandbox Code Playgroud)


Abo*_*tef 6

要调用此方法,您应该首先实现

titleForHeaderInSection
Run Code Online (Sandbox Code Playgroud)

然后tableView(tableView: UITableView, willDisplayHeaderView view: UIView, forSection section: Int)将调用方法

Swift解决方案:

func tableView(tableView: UITableView, willDisplayHeaderView view: UIView, forSection section: Int) {
    if let headerView = view as? UITableViewHeaderFooterView {


        headerView.textLabel?.textAlignment = Localize().isRTL ? .Right : .Left
        headerView.textLabel?.text = partsDataSource[section]
        headerView.textLabel?.textColor = UIColor ( red: 0.0902, green: 0.2745, blue: 0.2745, alpha: 1.0 )
        headerView.textLabel?.font = UIFont(name: UIDecorator.sharedInstance.PRIMARY_FONT, size: 14.0)
        headerView.contentView.backgroundColor = UIDecorator.sharedInstance.currentTheme.lightShadeColor
    }
}

func tableView(tableView: UITableView, titleForHeaderInSection section: Int) -> String? {
    return " "
}
Run Code Online (Sandbox Code Playgroud)


Chr*_*ris 5

这是我的版本,您需要截取普通标题背景的屏幕截图,并将其1px宽的片段保存为"my_head_bg.png"并将其添加到项目中.这样,它看起来完全正常:

-(UIView *)tableView:(UITableView *)tableView viewForHeaderInSection:(NSInteger)section {
    UILabel *lbl = [[[UILabel alloc] init] autorelease];
    lbl.textAlignment = UITextAlignmentCenter;
    lbl.font = [UIFont fontWithName:@"Helvetica-Bold" size:18];
    lbl.text = @"My Centered Header";
    lbl.textColor = [UIColor whiteColor];
    lbl.shadowColor = [UIColor grayColor];
    lbl.shadowOffset = CGSizeMake(0,1);
    lbl.backgroundColor = [UIColor colorWithPatternImage:[UIImage imageNamed:@"my_head_bg"]];
    lbl.alpha = 0.9;
    return lbl;
}
Run Code Online (Sandbox Code Playgroud)