【问题标题】:Custom Tableview cell with multiline UILabel needs dynamic height具有多行 UILabel 的自定义 Tableview 单元格需要动态高度
【发布时间】:2023-04-09 04:54:02
【问题描述】:

我使用 Interface Builder 创建了一个自定义 TableView 单元格。这是它的样子:

对于描述标签,我需要它来自动换行,所以我这样设置:

在我的 SettingsPageViewController 中,我重写了以下表格视图方法:

@implementation SBSettingsViewController
{
    NSArray *settingHeaders;
    NSArray *settingDescriptions;
}
- (void)viewDidLoad {
[super viewDidLoad];

[self setupLeftMenuButton];
// Do any additional setup after loading the view from its nib.
settingHeaders = [NSArray arrayWithObjects:@"Label Header 1",@"Label Header 2",nil];
settingDescriptions = [NSArray arrayWithObjects:@"Two line description Two line description Two line description ",@"One Line Description",nil];
}

- (void)didReceiveMemoryWarning {
[super didReceiveMemoryWarning];
// Dispose of any resources that can be recreated.
}

- (NSInteger)tableView:(UITableView *)tableView numberOfRowsInSection:(NSInteger)section
{
return [settingHeaders count];
}

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

SettingsTableViewCell *cell = (SettingsTableViewCell *)[tableView dequeueReusableCellWithIdentifier:simpleTableIdentifier];
if (cell == nil)
{
    NSArray *nib = [[NSBundle mainBundle] loadNibNamed:@"SettingsTableCell" owner:self options:nil];
    cell = [nib objectAtIndex:0];
}

cell.settingsHeaderLabel.text = [settingHeaders objectAtIndex:indexPath.row];
cell.settingsDescriptionLabel.text = [settingDescriptions objectAtIndex:indexPath.row];

cell.settingsDescriptionLabel.lineBreakMode = NSLineBreakByWordWrapping;
cell.settingsDescriptionLabel.numberOfLines = 0;
CGRect appFrame=[[UIScreen mainScreen] bounds];
cell.settingsDescriptionLabel.preferredMaxLayoutWidth = appFrame.size.width - 15;

[cell.settingsDescriptionLabel sizeToFit];
[cell.settingsDescriptionLabel setNeedsDisplay];
[cell layoutIfNeeded];

return cell;
}

-(void)tableView:(UITableView *)tableView willDisplayCell:(UITableViewCell *)cell forRowAtIndexPath:(NSIndexPath *)indexPath{

if ([tableView respondsToSelector:@selector(setSeparatorInset:)]) {
    [tableView setSeparatorInset:UIEdgeInsetsZero];
}

if ([tableView respondsToSelector:@selector(setLayoutMargins:)]) {
    [tableView setLayoutMargins:UIEdgeInsetsZero];
}

if ([cell respondsToSelector:@selector(setLayoutMargins:)]) {
    [cell setLayoutMargins:UIEdgeInsetsZero];
}
}

- (CGFloat)tableView:(UITableView *)tableView heightForRowAtIndexPath:(NSIndexPath *)indexPath
{
return 85;
}

生成的页面如下所示:

如您所见,第一个 tableview 单元格的描述标签没有自动换行。它只是切断。 如何将其包裹起来?

另外,我希望动态调整 Tableview 单元格的高度。我试图将 heightForRowAtIndexPath: 更改为 UITableViewAutomaticDimension 但这只是让它看起来非常奇怪:

如何调整表格视图高度,以使 1 行描述标签略短的行?

感谢您的帮助!

【问题讨论】:

标签: ios objective-c uitableview interface-builder


【解决方案1】:

将标题标签的顶部、前导、尾随到超级视图和底部(垂直)约束到描述标签。 与描述标签相同 > 超级视图的前导、尾随和下边距。

现在设置标题标签高度修复和描述标签使多行(Lines = 0)

对于在 viewDidLoad 中设置的表视图

_tableView.estimatedRowHeight = 100.0;
_tableView.rowHeight = UITableViewAutomaticDimension;

让我知道这是否有效...

【讨论】:

  • 初学者问题:如何从我的 XIB 文件中连接我的 tableView 以便 ViewController 可以访问它?我有数据源和委托连接到文件的所有者。我还有什么需要做的吗?
  • 这项工作我遵循了上面推荐的一些教程,并将我在 XIB 中的 tableview 中的 IBOutlet 添加到我的 viewcontroller.m 文件中......谢谢大家!
猜你喜欢
  • 1970-01-01
  • 1970-01-01
  • 1970-01-01
  • 1970-01-01
  • 2017-11-14
  • 1970-01-01
  • 2011-04-13
  • 2011-11-27
  • 1970-01-01
相关资源
最近更新 更多