iOS7 - TableView Header中的多行文本?

11

我正在尝试将我的一个应用程序的代码更新到iOS7,并将其转换为使用Storyboards。在尝试使表视图标题显示多行文本时遇到了问题(表视图单元格仍然正常工作)。

TableView是使用Storyboard创建的,但我正在使用旧程序中的自定义单元格而不是在Storyboard中构建新的自定义单元格。

我之前运行良好的代码:

- (NSString *)tableView:(UITableView *)tableView titleForHeaderInSection:(NSInteger)section
{

switch (section)
{
    case 0:
        return [NSString stringWithFormat:@"Fixture Counts per \nTable 2902.1 - %@ IBC\n\n    Occupancy Recap", strCodeYear ]; //(rounded up)
        break;
    case 1:
        return [NSString stringWithFormat:@"Womens Fixtures"];
        break;
    case 2:
        return [NSString stringWithFormat:@"Mens Fixtures"];
        break;
    case 3:
        return [NSString stringWithFormat:@"General Fixtures"];
        break;
}
// To avoid "control reaches end of non-void function"
return 0;
}

\n以前足以使标题根据文本行扩展。我可以看出\n仍然会引发换行,但只有一行文本可见。

我已调整标题大小以确保有足够的空间,但仍然只有一行文本:

// Set Custom Heights for Headers
-(CGFloat)tableView:(UITableView *)tableView heightForHeaderInSection:(NSInteger)section{
if(section == 0 )
    return 200.0f;
else return 60.0f; // put 22 in case of plain one..
}

我试图找到一种像对待标签一样使用 numberOfLines 的方法,但没有成功。我查阅了 UITableView 类参考文档,但没有找到与行数相关的内容,不过还是尝试了一下:

- (UIView *) tableView:(UITableView *)tableView viewForHeaderInSection:(NSInteger)section
{
UILabel *label = [[UILabel alloc] init];
label.numberOfLines = 2;
return 0; // nil 0 view section
}

非常感谢您的帮助。


1
我很惊讶你的 tableView:viewForHeaderInSection: 方法没有给出任何警告,却仍然能够获取到文本。你应该返回一个视图,而不是返回 nil - nhgrif
4个回答

6

这是我的做法,我得到了一个多行表头的结果:

结果

多行表头

源代码

- (void)viewDidLoad
{
    [super viewDidLoad];
    // Do any additional setup after loading the view, typically from a nib.

    self.dataSource = @[@"apple", @"banana", @"orange", @"grapes", @"watermelon"];
    self.sectionNames = @[
                          @"Fixture Counts per \nTable 2902.1 - 2014 IBC\n\n    Occupancy Recap",
                          @"Women Fixtures",
                          @"Mens Fixture",
                          @"General Fixtures",
                          ];

    UITableView *tableView = [[UITableView alloc] initWithFrame:self.view.frame];
    tableView.delegate = self;
    tableView.dataSource = self;

    [self.view addSubview:tableView];
}

-(NSInteger)numberOfSectionsInTableView:(UITableView *)tableView
{
    return self.sectionNames.count;
}

-(NSInteger)tableView:(UITableView *)tableView numberOfRowsInSection:(NSInteger)section
{
    if(section == 0)
    {
        return self.dataSource.count;
    }
    else
    {
        return 0;
    }
}

-(UITableViewCell *)tableView:(UITableView *)tableView cellForRowAtIndexPath:(NSIndexPath *)indexPath
{
    static NSString *cellID = @"cellID";

    UITableViewCell *cell = [tableView dequeueReusableCellWithIdentifier:cellID];

    if(cell == nil)
    {
        cell = [[UITableViewCell alloc] initWithStyle:UITableViewCellStyleDefault reuseIdentifier:cellID];
    }

    cell.textLabel.text = [self.dataSource objectAtIndex:indexPath.row];

    return cell;
}



-(CGFloat)tableView:(UITableView *)tableView heightForHeaderInSection:(NSInteger)section
{
    // calculate height of UILabel
    UILabel *lblSectionName = [[UILabel alloc] init];
    lblSectionName.text = [self.sectionNames objectAtIndex:section];
    lblSectionName.textColor = [UIColor lightGrayColor];
    lblSectionName.numberOfLines = 0;
    lblSectionName.lineBreakMode = NSLineBreakByWordWrapping;
    lblSectionName.backgroundColor = [UIColor grayColor];

    [lblSectionName sizeToFit];

    return lblSectionName.frame.size.height;
}

-(UIView *)tableView:(UITableView *)tableView viewForHeaderInSection:(NSInteger)section
{
    // calculate height of UILabel
    UILabel *lblSectionName = [[UILabel alloc] init];
    lblSectionName.text = [self.sectionNames objectAtIndex:section];
    lblSectionName.textColor = [UIColor lightGrayColor];
    lblSectionName.numberOfLines = 0;
    lblSectionName.lineBreakMode = NSLineBreakByWordWrapping;
    lblSectionName.backgroundColor = [UIColor grayColor];

    [lblSectionName sizeToFit];

    return lblSectionName;
}

谢谢 - 我已经成功让它工作了。我最终注释掉了titleForHeaderInSection并将开关放在viewForHeaderSection中。 - Michael at AppsToKnow

6

将TableView设置为在“内容”下显示“静态单元格”,并在“样式”下选择“分组”。

然后您可以自动拥有多行标题和页脚。


1
如果有人遇到类似问题,这是我用于tableview部分的代码,包括我注释掉的部分:
#pragma mark - Table view data source

- (NSInteger)numberOfSectionsInTableView:(UITableView *)tableView
{
// Return the number of sections.
return 4;
}


// Set Custom Heights for Headers
-(CGFloat)tableView:(UITableView *)tableView heightForHeaderInSection:(NSInteger)section{
if(section == 0 )
    return 100.0f;
else return 60.0f;
}


- (NSInteger)tableView:(UITableView *)tableView numberOfRowsInSection:(NSInteger)section
{
switch (section)
{
    case 0: // Recap
        return 2;
        break;
    case 1: // Womens
        return 5;
        break;
    case 2: // Mens
        return 5;
        break;
    case 3: // General
        return 6;
        break;
}

return 0;
}

// ^.^ Customize the section title
//  -

/*
- (NSString *)tableView:(UITableView *)tableView titleForHeaderInSection:(NSInteger)section
{

switch (section)
{
    case 0:
        return [NSString stringWithFormat:@"Fixture Counts (rounded up) per \nTable 2902.1 - %@ IBC\n\n    Occupancy Recap", strCodeYear ];
        break;
    case 1:
        return [NSString stringWithFormat:@"Womens Fixtures"];
        break;
    case 2:
        return [NSString stringWithFormat:@"Mens Fixtures"];
        break;
    case 3:
        return [NSString stringWithFormat:@"General Fixtures"];
        break;
}
// To avoid "control reaches end of non-void function"
return 0;
}
*/


- (UIView *) tableView:(UITableView *)tableView viewForHeaderInSection:(NSInteger)section
{
// calculate height of UILabel
UILabel *lblSectionName = [[UILabel alloc] init];
// lblSectionName.textColor = [UIColor lightGrayColor];
lblSectionName.numberOfLines = 0;
lblSectionName.lineBreakMode = NSLineBreakByWordWrapping;
lblSectionName.backgroundColor = [UIColor colorWithRed:245.0f/255.0f green:245.0f/255.0f blue:245.0f/255.0f alpha:0.9f];

// lblSectionName.text
switch (section)
{
    case 0:
        lblSectionName.text = [NSString stringWithFormat:@"Fixture Counts (rounded up) per \nTable 2902.1 - %@ IBC\n\n    Occupancy Recap", strCodeYear ];
        break;
    case 1:
        lblSectionName.text = [NSString stringWithFormat:@"Womens Fixtures"]; 
        break;
    case 2:
        lblSectionName.text = [NSString stringWithFormat:@"Mens Fixtures"]; 
        break;
    case 3:
        lblSectionName.text = [NSString stringWithFormat:@"General Fixtures"];
        break;
}

[lblSectionName sizeToFit];

return lblSectionName;
}

// ^.^ Customize the appearance of table view cells.
//  -
- (UITableViewCell *)tableView:(UITableView *)tableView cellForRowAtIndexPath:(NSIndexPath *)indexPath
{
// UITableViewCell *cell = [tableView dequeueReusableCellWithIdentifier:@"cell" forIndexPath:indexPath];

// ^.^ Utilize the CustomCell class
//  -

static NSString *CustomCellIdentifier = @"CustomCellIdentifier";

CustomCell *cell = (CustomCell *)[tableView dequeueReusableCellWithIdentifier: CustomCellIdentifier];

if (cell == nil)  {
    NSArray *nib = [[NSBundle mainBundle] loadNibNamed:@"CustomCell" owner:self options:nil];
    for (id oneObject in nib)
        if ([oneObject isKindOfClass:[CustomCell class]])
            cell = (CustomCell *)oneObject;
}

// Identify Section and Row
NSUInteger section = [indexPath section];
NSUInteger row = [indexPath row];

// Sets the number of lines in the cell to be a variable
cell.titleLabel.numberOfLines = 0;

switch (section)
{
    case 0:
        cell.titleLabel.text = [titleFixOccupancyArray objectAtIndex:row];
        cell.dataLabel.text = [dataFixOccupancyArray objectAtIndex:row];
        cell.warningLabel.text = [warningFixOccupancyArray objectAtIndex:row];
        break;
    case 1:
        cell.titleLabel.text = [titleWomensArray objectAtIndex:row];
        cell.dataLabel.text = [dataWomensArray objectAtIndex:row];
        cell.warningLabel.text = [warningWomensArray objectAtIndex:row];
        break;
    case 2:
        cell.titleLabel.text = [titleMensArray objectAtIndex:row];
        cell.dataLabel.text = [dataMensArray objectAtIndex:row];
        cell.warningLabel.text = [warningMensArray objectAtIndex:row];
        break;
    case 3:
        cell.titleLabel.text = [titleFixturesArray objectAtIndex:row];
        cell.dataLabel.text = [dataFixturesArray objectAtIndex:row];
        cell.warningLabel.text = [warningFixturesArray objectAtIndex:row];
        break;  
}

return cell;
}

1

在 @Zhang 的推荐下,我更喜欢使用 NSString 的 boundRectWithSize 方法来计算标题的高度:

- (UIView *)tableView:(UITableView *)tableView viewForHeaderInSection:(NSInteger)section {

    // create a custom multi-line label
    UILabel *label = [[UILabel alloc] init];
    label.text = self.headerText;
    label.numberOfLines = 0;
    label.textAlignment = NSTextAlignmentCenter;
    label.lineBreakMode = NSLineBreakByWordWrapping;
    label.font = self.headerFont;
    [label sizeToFit];

    return label;
}

- (CGFloat)tableView:(UITableView *)tableView heightForHeaderInSection:(NSInteger)section {

    // compute size
    NSString *text = self.headerText
    CGRect rect = [text boundingRectWithSize:CGSizeMake(CGRectGetWidth(tableView.frame), CGFLOAT_MAX) options:NSStringDrawingUsesLineFragmentOrigin attributes:@{NSFontAttributeName : self.headerFont} context:nil];

    return MAX(CGRectGetHeight(rect), 44); // keep height no smaller than Plain cells
}

网页内容由stack overflow 提供, 点击上面的
可以查看英文原文,
原文链接