具有动态单元格高度的UITableview中的AutoLayout

对于我的表视图单元格的动态高度,我从此链接中获取参考。 在UITableView中使用自动布局来获取动态单元格布局和可变行高

这是具有所有约束的自定义单元格

这是我的tableview数据源代码和委托方法

-(NSInteger)tableView:(UITableView *)tableView numberOfRowsInSection:(NSInteger)section; { return arrTemp. count; } - (UITableViewCell *)tableView:(UITableView *)tableView cellForRowAtIndexPath:(NSIndexPath *)indexPath { static NSString *cellIdentifier=@"AutoLAyoutCell"; AutoLayoutTableViewCell *cell=(AutoLayoutTableViewCell *)[tableView dequeueReusableCellWithIdentifier:cellIdentifier]; if (cell==nil) { for (id currentObject in [[NSBundle mainBundle] loadNibNamed:@"AutoLayoutTableViewCell" owner:self options:nil]) { if ([currentObject isKindOfClass:[UITableViewCell class]]) { cell = (AutoLayoutTableViewCell *)currentObject; break; } } } cell.IBlblLineNo.text=[NSString stringWithFormat:@"Line:%i",indexPath.row]; cell.IBlblLineText.text=[arrTemp objectAtIndex:indexPath.row]; [cell setNeedsUpdateConstraints]; [cell updateConstraintsIfNeeded]; CGSize expectedlineLabelSize = [cell.IBlblLineText.text sizeWithFont:cell.IBlblLineText.font constrainedToSize:CGSizeMake(280, 1000) lineBreakMode:NSLineBreakByTruncatingTail]; cell.IBlblLineText.numberOfLines=expectedlineLabelSize.height/17; CGRect frmlbl=cell.IBlblLineText.frame; frmlbl.size.height=expectedlineLabelSize.height; cell.IBlblLineText.frame=frmlbl; return cell; } - (CGFloat)tableView:(UITableView *)tableView heightForRowAtIndexPath:(NSIndexPath *)indexPath { AutoLayoutTableViewCell *cell = (AutoLayoutTableViewCell *)[IBtblAutoLayoutExample cellForRowAtIndexPath:indexPath]; cell.IBlblLineNo.text=[NSString stringWithFormat:@"Line:%i",indexPath.row]; cell.IBlblLineText.text=[arrTemp objectAtIndex:indexPath.row]; [cell setNeedsUpdateConstraints]; [cell updateConstraintsIfNeeded]; CGSize expectedlineLabelSize = [cell.lineLabel.text sizeWithFont:cell.lineLabel.font constrainedToSize:CGSizeMake(280, 1000) lineBreakMode:NSLineBreakByWordWrapping]; cell.IBlblLineText.numberOfLines=expectedlineLabelSize.height/17; CGRect frmlbl=cell.IBlblLineText.frame; frmlbl.size.height=expectedlineLabelSize.height; cell.IBlblLineText.frame=frmlbl; CGFloat height = [cell.contentView systemLayoutSizeFittingSize:UILayoutFittingCompressedSize].height; height += 1.0f; return height; } - (CGFloat)tableView:(UITableView *)tableView estimatedHeightForRowAtIndexPath:(NSIndexPath *)indexPath { AutoLayoutTableViewCell *cell = (AutoLayoutTableViewCell *)[IBtblAutoLayoutExample cellForRowAtIndexPath:indexPath]; CGSize expectedlineLabelSize = [cell.IBlblLineText.text sizeWithFont:cell.IBlblLineText.font constrainedToSize:CGSizeMake(280, 1000) lineBreakMode:NSLineBreakByTruncatingTail]; return expectedlineLabelSize.height; } 

我有两个问题:

  1. 我的问题是我EXE_BAD_EXCESS附近收到错误EXE_BAD_EXCESS

     AutoLayoutTableViewCell *cell = (AutoLayoutTableViewCell *)[IBtblAutoLayoutExample cellForRowAtIndexPath:indexPath]; 

    heightForRowAtIndexPathestimatedHeightForRowAtIndexPath

  2. 为什么我必须在cellForRowAtIndexPathheightForRowAtIndexPath编写标签文本?

另外,我错过了为细胞达到动态高度所需的一切吗?

要设置行高和估计行高的自动尺寸,请确保执行以下步骤,自动尺寸对单元格/行高度布局有效。

  • 分配并实现tableview dataSource和delegate
  • UITableViewAutomaticDimension分配给rowHeight和estimatedRowHeight
  • 实现delegate / dataSource方法(即heightForRowAt并向其返回值UITableViewAutomaticDimension

目标C:

 // in ViewController.h #import  @interface ViewController : UIViewController  @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 // Swift 4.2 onwards table.rowHeight = UITableView.automaticDimension table.estimatedRowHeight = UITableView.automaticDimension // Swift 4.1 and below table.rowHeight = UITableViewAutomaticDimension table.estimatedRowHeight = UITableViewAutomaticDimension } // UITableViewAutomaticDimension calculates height of label contents/text func tableView(_ tableView: UITableView, heightForRowAt indexPath: IndexPath) -> CGFloat { // Swift 4.2 onwards return UITableView.automaticDimension // Swift 4.1 and below return UITableViewAutomaticDimension } 

对于UITableviewCell中的标签实例

  • 设置行数= 0(&换行模式=截断尾部)
  • 设置相对于其superview / cell容器的所有约束(顶部,底部,右侧)。
  • 可选 :如果您希望标签覆盖最小垂直区域,即使没有数据,也可以设置标签的最小高度。

在此处输入图像描述

注意 :如果您有多个具有动态长度的标签(UIElements),应根据其内容大小进行调整:调整要以更高优先级展开/压缩的标签的“内容拥抱和压缩阻力优先级”。