Minimum cell height with UITableViewAutomaticDimension

青春壹個敷衍的年華 提交于 2020-06-07 09:45:49

问题


Is it possible to set minimal height for cell? I use dynamic:

tableView.estimatedRowHeight = 83.0
tableView.rowHeight = UITableViewAutomaticDimension

But I need to set minimal height for cell when my news title label text is in one line.


回答1:


Have you tried creating a constraint in your custom UITableViewCell's view of height >= 60.0?




回答2:


Got it. Made it work as below.

Drag and drop a View on top of UITableViewCell and set constraints Leading, trailing, top and Bottom as 0. Set height constraint as >= ExpectedMinimumSize.

In heightForRowAtIndexPath Delegatemethod:

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

In ViewDidLoad:

self.tableView.estimatedRowHeight = 60; // required value.



回答3:


There is a trick which is answered by @Hytek. For this you have to give the constraint for minimum height.

For example: If there is one UILabel into your table cell and you want that UILabel increase the height as per the dynamic content. And you have code it like below.

tableView.estimatedRowHeight = 83.0
tableView.rowHeight = UITableViewAutomaticDimension

It will increase your label height when content is bigger but it also will decrease when your content is small. So if you expect that label should have minimum height then you have to give a height constraint to your UILabel in a way that height >= 30.0 to your label.

This way your UILabel will not decrease the height less then 30.0.




回答4:


At the auto layout code of the custom cell (either Interface Builder or programmatically), add the appropriate constraints.

E.g. (Programmatically in custom cell)

    UILabel * label = [UILabel new];
    [self.contentView addSubview:label];
    NSDictionary * views = NSDictionaryOfVariableBindings(label);
//Inset 5 px
    [self.contentView addConstraints:[NSLayoutConstraint constraintsWithVisualFormat:@"H:|-5-[label]-5-|" options:0 metrics:nil views:views]];
    [self.contentView addConstraints:[NSLayoutConstraint constraintsWithVisualFormat:@"V:|-5-[label]-5-|" options:0 metrics:nil views:views]];
// height >= 44
    [self.contentView addConstraint:[NSLayoutConstraint constraintWithItem:self.mainLabel attribute:NSLayoutAttributeHeight relatedBy:NSLayoutRelationGreaterThanOrEqual toItem:nil attribute:NSLayoutAttributeNotAnAttribute multiplier:1.0 constant:44.0]];



回答5:


override func tableView(_ tableView: UITableView, heightForRowAt indexPath: IndexPath) -> CGFloat {
    return (UITableView.automaticDimension > minimumHeight) ? UITableView.automaticDimension : minimumHeight
}



回答6:


Set a contentViews heightAnchor to your least required height .

Swift 4.2 version programatically

contentView.heightAnchor.constraint(greaterThanOrEqualToConstant: <Required least Height>).isActive = true



来源:https://stackoverflow.com/questions/35169125/minimum-cell-height-with-uitableviewautomaticdimension

易学教程内所有资源均来自网络或用户发布的内容,如有违反法律规定的内容欢迎反馈
该文章没有解决你所遇到的问题?点击提问,说说你的问题,让更多的人一起探讨吧!