OGeek|极客世界-中国程序员成长平台

标题: ios - 具有多行 UILabel 的自定义 Tableview 单元格需要动态高度 [打印本页]

作者: 菜鸟教程小白    时间: 2022-12-12 18:20
标题: ios - 具有多行 UILabel 的自定义 Tableview 单元格需要动态高度

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

IB Custom Table Cell

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

Settings for Description Label

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

@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)tableViewUITableView *)tableView numberOfRowsInSectionNSInteger)section
{
return [settingHeaders count];
}

- (UITableViewCell *)tableViewUITableView *)tableView cellForRowAtIndexPathNSIndexPath *)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)tableViewUITableView *)tableView willDisplayCellUITableViewCell *)cell forRowAtIndexPathNSIndexPath *)indexPath{

if ([tableView respondsToSelectorselector(setSeparatorInset]) {
    [tableView setSeparatorInset:UIEdgeInsetsZero];
}

if ([tableView respondsToSelectorselector(setLayoutMargins]) {
    [tableView setLayoutMargins:UIEdgeInsetsZero];
}

if ([cell respondsToSelectorselector(setLayoutMargins]) {
    [cell setLayoutMargins:UIEdgeInsetsZero];
}
}

- (CGFloat)tableViewUITableView *)tableView heightForRowAtIndexPathNSIndexPath *)indexPath
{
return 85;
}

生成的页面如下所示:

enter image description here

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

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

enter image description here

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

感谢您的帮助!



Best Answer-推荐答案


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

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

对于在 viewDidLoad 中设置的 TableView

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

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

关于ios - 具有多行 UILabel 的自定义 Tableview 单元格需要动态高度,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/34959491/






欢迎光临 OGeek|极客世界-中国程序员成长平台 (http://sqlite.in/) Powered by Discuz! X3.4