gpt4 book ai didi

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

转载 作者:技术小花猫 更新时间:2023-10-29 11:03:32 26 4
gpt4 key购买 nike

我正在尝试将我的一个应用程序中的代码更新到 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/

26 4 0
Copyright 2021 - 2024 cfsdn All Rights Reserved 蜀ICP备2022000587号
广告合作:1813099741@qq.com 6ren.com