UITableViewCell最小高度,自动调整单元格大小

时间:2016-04-22 22:33:01

标签: ios swift uitableview autolayout tableviewcell

我正在使用我的项目中自动调整单元格的大小。我想将单元格保持在最低height,但我无法为单元格设置最小height 约束 >在我的内容视图中(因为它在添加新约束视图中显示为灰色:

enter image description here

是否有为我的单元格添加最低height 约束

7 个答案:

答案 0 :(得分:7)

启用UITableViewAutomaticDimension后,系统会调用UITableViewCell的{​​{3}}方法来计算单元格高度,因此您可以继承UITableViewCell并重写此方法以强制最小化单元格的高度,就这样

class MinHeightTableViewCell: UITableViewCell {

    var minHeight: CGFloat?

    override func systemLayoutSizeFitting(_ targetSize: CGSize, withHorizontalFittingPriority horizontalFittingPriority: UILayoutPriority, verticalFittingPriority: UILayoutPriority) -> CGSize {
        let size = super.systemLayoutSizeFitting(targetSize, withHorizontalFittingPriority: horizontalFittingPriority, verticalFittingPriority: verticalFittingPriority)
        guard let minHeight = minHeight else { return size }
        return CGSize(width: size.width, height: max(size.height, minHeight))
    }
}

然后

public func tableView(_ tableView: UITableView, cellForRowAt indexPath: IndexPath) -> UITableViewCell {
    let cell = tableView.dequeueReusableCell(withIdentifier: "minHeightCell", for: indexPath) as! MinHeightTableViewCell
    cell.minHeight = 62 // change with your desidered min height
    cell.textLabel?.text = "some text"
    return cell
}

答案 1 :(得分:2)

这是在viewDidLoad中使用的代码。 cell是UITableViewCell。请注意,relatedBy参数为> =。 44是我的最低身高...

[cell.contentView addConstraint: [NSLayoutConstraint constraintWithItem: cell.contentView attribute: NSLayoutAttributeHeight relatedBy: NSLayoutRelationGreaterThanOrEqual toItem: nil attribute: NSLayoutAttributeNotAnAttribute multiplier: 1 constant: 44]];

答案 2 :(得分:1)

通过返回tableview的UITableViewAutomaticDimension高度计算方法,可以使单元格成为动态高度。但为此,您的单元格设计约束应该类似于下面的

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


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

enter image description here

注意:Todo Title是静态高度。并且描述将是动态高度。因此描述不具有高度约束。重要的是您需要为描述标签

设置多行为0

答案 3 :(得分:0)

将高度限制设置为height >= 60.0无需执行任何操作

答案 4 :(得分:0)

简单添加高度约束,使用您想要的最小高度,然后将其关系更改为“大于等于”。看一下屏幕短片的突出部分。 enter image description here

答案 5 :(得分:0)

按照以下步骤,为具有最小高度的单元格设置自动尺寸。

  • 分配并实施tableview dataSource和delegate
  • UITableViewAutomaticDimension分配给rowHeight& estimatedRowHeight
  • 实施委托/数据源方法(即heightForRowAt并向其返回值UITableViewAutomaticDimension

-

目标C:

// in ViewController.h
#import <UIKit/UIKit.h>

@interface ViewController : UIViewController <UITableViewDelegate, UITableViewDataSource>

  @property IBOutlet UITableView * table;

@end

// in ViewController.m

- (void)viewDidLoad {
    [super viewDidLoad];
    self.table.dataSource = self;
    self.table.delegate = self;

    self.table.rowHeight = UITableViewAutomaticDimension;
    self.table.estimatedRowHeight = UITableViewAutomaticDimension;
}

-(CGFloat)tableView:(UITableView *)tableView heightForRowAtIndexPath:(NSIndexPath *)indexPath {

    return UITableViewAutomaticDimension;
}

<强>夫特:

@IBOutlet weak var table: UITableView!

override func viewDidLoad() {
    super.viewDidLoad()

    // Don't forget to set dataSource and delegate for table
    table.dataSource = self
    table.delegate = self

    // Set automatic dimensions for row height
    table.rowHeight = UITableViewAutomaticDimension
    table.estimatedRowHeight = UITableViewAutomaticDimension
}



// UITableViewAutomaticDimension calculates height of label contents/text
func tableView(_ tableView: UITableView, heightForRowAt indexPath: IndexPath) -> CGFloat {
    return UITableViewAutomaticDimension
}

用于UITableviewCell中的标签实例

  • 设置行数= 0(&amp; line break mode = truncate tail)
  • 相对于其superview / cell容器设置所有约束(顶部,底部,右侧)。
  • 注意:如果您希望标签覆盖最小垂直区域,即使没有数据,也要设置标签的最小高度。

enter image description here

答案 6 :(得分:0)

我将此约束添加到了单元格中,但是在代码中(快速)

contentView.heightAnchor.constraint(equalToConstant: 80).isActive = true

如果您的单元格视图需要更多空间:

contentView.heightAnchor.constraint(greaterThanOrEqualToConstant: 80).isActive = true