我正在使用我的项目中自动调整单元格的大小。我想将单元格保持在最低height
,但我无法为单元格设置最小height
约束 >在我的内容视图中(因为它在添加新约束视图中显示为灰色:
是否有为我的单元格添加最低height
约束?
答案 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;
}
注意:Todo Title是静态高度。并且描述将是动态高度。因此描述不具有高度约束。重要的是您需要为描述标签
设置多行为0答案 3 :(得分:0)
将高度限制设置为height >= 60.0
无需执行任何操作
答案 4 :(得分:0)
答案 5 :(得分:0)
按照以下步骤,为具有最小高度的单元格设置自动尺寸。
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中的标签实例
答案 6 :(得分:0)
我将此约束添加到了单元格中,但是在代码中(快速)
contentView.heightAnchor.constraint(equalToConstant: 80).isActive = true
如果您的单元格视图需要更多空间:
contentView.heightAnchor.constraint(greaterThanOrEqualToConstant: 80).isActive = true