iOS7 - TableView标题中的多行文字?

Mic*_*now 10 objective-c uitableview ios

我正在尝试将我的一个应用程序中的代码更新到iOS7,我正在将其转换为使用Storyboard.我试图获得一个表格视图标题来显示多行文本(表格视图单元格仍能正常工作).

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;
}
Run Code Online (Sandbox Code Playgroud)

\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..
}
Run Code Online (Sandbox Code Playgroud)

我试图找到一种方法来使用numberOfLines,比如标签,但没有成功.我查看了UITableView类参考,并没有看到引用行数的任何内容,但我还是尝试了:

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

任何帮助表示赞赏.

coo*_*994 6

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

然后您就可以自动拥有多行页眉和页脚。


Zha*_*ang 5

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

结果

多行表标题

源代码

- (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;
}
Run Code Online (Sandbox Code Playgroud)