• 设为首页
  • 点击收藏
  • 手机版
    手机扫一扫访问
    迪恩网络手机版
  • 关注官方公众号
    微信扫一扫关注
    迪恩网络公众号

ios - 具有多行 UILabel 的自定义 Tableview 单元格需要动态高度

[复制链接]
菜鸟教程小白 发表于 2022-12-12 18:20:38 | 显示全部楼层 |阅读模式 打印 上一主题 下一主题

我使用 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/

回复

使用道具 举报

懒得打字嘛,点击右侧快捷回复 【右侧内容,后台自定义】
您需要登录后才可以回帖 登录 | 立即注册

本版积分规则

关注0

粉丝2

帖子830918

发布主题
阅读排行 更多
广告位

扫描微信二维码

查看手机版网站

随时了解更新最新资讯

139-2527-9053

在线客服(服务时间 9:00~18:00)

在线QQ客服
地址:深圳市南山区西丽大学城创智工业园
电邮:jeky_zhao#qq.com
移动电话:139-2527-9053

Powered by 互联科技 X3.4© 2001-2213 极客世界.|Sitemap