当前位置: 首页 > 知识库问答 >
问题:

具有动态单元格高度的 UITable视图中的自动布局

孙正业
2023-03-14

对于我的表视图单元格的动态高度,我从此链接中引用。在 UITableView 中使用自动布局进行动态单元格布局

这是我的表视图数据源和委托方法的代码

-(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;

}

我有 2 个问题:

>

  • 我的问题是我在线路附近EXE_BAD_EXCESS收到错误

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

    heightForRowAtIndexPath中估计HeightForRowAtIndexPath

    为什么我必须在单元格中同时写入标签文本“用于”用于“对于”内容索引路径“和”高度“对于”行列索引路径“

    另外,我是否缺少实现单元动态高度所需的任何内容?

  • 共有1个答案

    贺皓
    2023-03-14

    设置行高的自动尺寸

    • 分配和实现表视图数据源和委托
    • UITableView自动维度指定为行高度

    -

    目标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;
    }
    

    Swift:

    @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 (

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

     类似资料:
    • 我有一个带有一堆单元格的表视图(自定义单元格,它只有它的内容视图)。 在 中,我正在向自定义单元格的content视图添加一个预定义的UIView(它有几个子视图)。我之前为UIView及其子视图设置了所有约束。 最后但并非最不重要的是,我为我的自定义单元格的内容视图(superview)和UIView设置了垂直和水平约束,UIView是在(subview)之前添加的。 约束字符串如下所示: 不幸

    • 我有一个UIScrollView,嵌套在一个内容视图中,它有两个嵌套的子视图,一个具有已知高度的常规UIView,以及一个具有动态高度的容器视图,具体取决于内容。就像这样: 视图如下所示: 我的约束设置如下: 滚动视图被限制在其超级视图(即视图)的尾缘、前缘、顶端和下边缘 内容视图被约束到其超级视图(即滚动视图)的尾部、前导、顶部和底部边缘。它还具有与主视图(即视图)相等的宽度约束,因此滚动视图的

    • 我正在研究具有动态单元格高度的表视图。 由于我的要求,我必须展示40种不同的cell.Each细胞是不同的。 例如。 一个单元有2个标签,一个图像视图另一个单元只有2个图像视图,另一个单元格有5个标签,1个文本视图,2个按钮,2个图像查看 我的问题是我无法静态定义高度。每个单元格中的.labels和textview都基于运行时的文本。因此,根据各个元素,我需要调整高度。所以我根据下面的编码计算了标

    • 我在UIViewController中有一个UITableView作为视图的一部分。我正在尝试根据文本的长度自动调整表格单元格的大小。它不会设置textview的文本,单元格将以idCellTextView单元格的标准高度结束。我已经四处搜索过,正在尝试从单元格内的文本视图相对于内容视图和代码在自动布局中使用固定,如下所示: 与问题相关的视图控制器的其余部分显示在以下位置:

    • 我正在设计一个具有滚动视图的页面,其上方是表格视图(禁用滚动)。为此,我在这个问题中提到了答案 - 使UITableView不可滚动并调整高度以容纳所有单元格,但没有成功。 视图层次结构以及提供的约束- -主视图 -滚动视图< br >固定在主视图的所有边上(0,0,0,0),限制边距 -内容视图 固定到滚动视图(0,0,0,0),与主视图宽度相等,与主视图高度相等(优先级-250) -内容视图中

    • 我使用自动布局的动态高度表格单元格,在表格单元格内部,有1个动态高度UILabel和1个动态UIView,其中包括多个UIImageView。 这是布局:情节提要屏幕截图 TableCell: 但我犯了这个错误: 请帮助我,我错过了什么。提前谢谢你。