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

时间:2016-01-23 04:05:21

标签: ios objective-c uitableview interface-builder

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

IB Custom Table Cell

对于描述标签,我需要自动换行,所以我将其设置为:

Settings for Description Label

在我的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;
}

以下是生成的页面的样子:

enter image description here

如您所见,第一个tableview单元格的描述标签不会自动换行。它只是切断了。 如何包装?

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

enter image description here

如何调整行视图高度,1行说明标签略短?

感谢您的帮助!

1 个答案:

答案 0 :(得分:2)

给标题标签top,leading,trailing to super view和bottom(vertical)to description Label。 同样的描述标签>超前视图的前沿,后沿和下边距。

现在设置标题标签高度修复和描述标签make multiline(Lines = 0)

用于viewDidLoad中设置的表视图

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

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