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

标签 ios objective-c uitableview

我正在尝试将我的一个应用程序中的代码更新到 iOS7,并将其转换为使用 Storyboard。我在尝试让 TableView 标题显示多行文本时遇到了障碍( TableView 单元格仍然可以正常工作)。

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 Class Reference 并没有看到任何引用行数的内容,但我还是尝试了:

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

感谢任何帮助。

最佳答案

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

结果

Multiline Table Header

源代码

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

关于iOS7 - TableView 标题中的多行文本?,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/24542995/

相关文章:

ios - 如何强制 iOS 应用程序挂起而不是崩溃?

ios - 保存高质量图像,进行实时处理——什么是最好的方法?

iphone - 如何从使用 JSONKit 创建的 NSDictionary 正确填充 UITableView NSArray?

iphone - 无法使用@interface将对象添加到NSMutableArray

ios - Swift UITableView 每 N 个单元格不同

ios - 缺少代码签名权利 - 在 bundle 'com.google.Googleplus' 中找不到任何权利

objective-c - 带旋转的 MKAnnotationView 图像

c++ - Objective-C block 和 C++ 对象

objective-c - 为什么单击表格单元格时无法添加加载微调器?

swift - 无法在侧边菜单中填充表格 View 单元格