首页 文章

UITableView单元的动态高度问题(Swift)

提问于
浏览
45

可变长度的动态文本被注入tableview单元格标签 . 为了使tableview单元格的高度动态调整,我在_1553264中实现了:

self.tableView.estimatedRowHeight = 88.0
self.tableView.rowHeight = UITableViewAutomaticDimension

这适用于尚未滚动到的单元格(当滚动到单元格时调用 UITableViewAutomaticDimention ),但不适用于在加载带有数据的表格时最初在屏幕上呈现的单元格 .

我试过简单地重新加载数据(如许多其他资源中所建议的):

self.tableView.reloadData()

viewDidAppear()viewWillAppear() 中都无济于事 . 我迷了..有没有人知道如何让xcode渲染最初在屏幕上加载的单元格的动态高度?

如果有更好的替代方法,请告诉我 .

17 回答

  • 1

    我的灵感来自你的解决方案并尝试了另一种方式 .

    请尝试将 tableView.reloadData() 添加到 viewDidAppear() .

    这适合我 .

    我认为滚动背后的东西是"the same"作为reloadData . 滚动屏幕时,就像 viewDidAppear 时调用 reloadData() .

    如果这样做,PLZ回答这个答案,所以我可以肯定这个解决方案 .

  • 6

    要使UITableViewCell的自动调整工作,请确保您正在执行这些更改:

    • 在Storyboard中,你的UITableView应该只包含动态原型单元格(它不应该是_1553307工作 .

    • 在Storyboard中,您的UITableViewCell的UILabel已经为所有4个约束配置了top,bottom,leading和trailing约束 .

    • 在Storyboard中,您的UITableViewCell 's UILabel'的行数应为0

    • 在UITiewController的viewDidLoad函数中设置UITableView属性:

    self.tableView.estimatedRowHeight = <minimum cell height> 
    self.tableView.rowHeight = UITableViewAutomaticDimension
    
  • 74

    试试这个:

    func tableView(tableView: UITableView, heightForRowAtIndexPath indexPath: NSIndexPath) -> CGFloat {
        return UITableViewAutomaticDimension
    }
    

    EDIT

    func tableView(tableView: UITableView, estimatedHeightForRowAtIndexPath indexPath: NSIndexPath) -> CGFloat {
        return UITableViewAutomaticDimension
    }
    

    Swift 4

    func tableView(_ tableView: UITableView, estimatedHeightForRowAt indexPath: IndexPath) -> CGFloat {
        return UITableViewAutomaticDimension
    }
    

    Swift 4.2

    func tableView(_ tableView: UITableView, estimatedHeightForRowAt indexPath: IndexPath) -> CGFloat {
        return UITableView.automaticDimension
    }
    

    定义以上两种方法 .
    它解决了这个问题 .

    PS: Top and bottom constraints is required for this to work.

    Here is example

  • 10

    用这个:

    tableView.rowHeight = UITableViewAutomaticDimension
    tableView.estimatedRowHeight = 300
    

    并且不要使用: heightForRowAtIndexPath 委托功能

    此外,在故事板中不要设置包含大量数据的标签的高度 . 给它 top, bottom, leading, trailing 约束 .

  • 27

    SWIFT 3

    tableView.rowHeight = UITableViewAutomaticDimension
    tableView.estimatedRowHeight = 160
    

    和!!!在storyBoard中:您必须为Label设置TOP&BOTTOM约束 . 没有其他的 .

  • 4

    这个奇怪的bug是通过Interface Builder参数解决的,因为其他答案没有解决问题 .

    我所做的就是使默认标签尺寸大于可能的内容,并将其反映在 estimatedRowHeight 高度中 . 以前,我将Interface Builder中的默认行高设置为 88px ,并在我的控制器 viewDidLoad() 中反映出来:

    self.tableView.rowHeight = UITableViewAutomaticDimension
    self.tableView.estimatedRowHeight = 88.0
    

    But that didn't work. 所以我意识到内容不会比 100px 更大,所以我将默认单元格高度设置为 108px (大于潜在内容),并在控制器 viewDidLoad() 中反映出来:

    self.tableView.rowHeight = UITableViewAutomaticDimension
    self.tableView.estimatedRowHeight = 108.0
    

    这实际上允许代码 shrink down 初始标签到正确的大小 . 换句话说,它从未扩展到更大的尺寸,但总是可以缩小...而且, viewWillAppear() 中不需要额外的 self.tableView.reloadData() .

    I know this does not cover highly variable content sizes, but this worked in my situation where the content had a maximum possible character count.

    不确定这是否是Swift或Interface Builder中的错误,但它就像魅力一样 . 试试看!

  • -3

    UITableView的动态大小调整单元需要2件事

    • 在表格视图单元格中设置视图的右边约束(主要包括为视图提供正确的顶部,底部和traling约束)

    • 在viewDidLoad()中调用TableView的这些属性

    tableView.rowHeight = UITableViewAutomaticDimension
    
     tableView.estimatedRowHeight = 140
    

    This是一个关于使用swift 3编写的自调整大小(动态表视图单元格)的精彩教程 .

  • 2

    设置行高和估计行高的自动尺寸并确保以下步骤:

    @IBOutlet weak var table: UITableView!
    
    override func viewDidLoad() {
        super.viewDidLoad()
    
        // 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容器设置所有约束(顶部,底部,右侧) .

    • 可选:如果您希望标签覆盖最小垂直区域,即使没有数据,也可以设置标签的最小高度 .

    这是具有动态高度约束的样本标签 .

    enter image description here

  • 11

    对于Swift 3,您可以使用以下内容:

    func tableView(_ tableView: UITableView, heightForRowAt indexPath: IndexPath) -> CGFloat {
        return UITableViewAutomaticDimension
    }
    
    func tableView(_ tableView: UITableView, estimatedHeightForRowAt indexPath: IndexPath) -> CGFloat {
        return UITableViewAutomaticDimension
    }
    
  • 0

    尝试

    override func viewWillAppear(animated: Bool) {
        self.tableView.layoutSubviews()
    }
    

    我有同样的问题,它对我有用 .

  • 7

    对于Swift,我在iOS 9.0和iOS 11中也检查了这个答案(Xcode 9.3)

    func tableView(tableView: UITableView, heightForRowAtIndexPath indexPath: NSIndexPath) -> CGFloat {
        return UITableViewAutomaticDimension
    }
    
    func tableView(_ tableView: UITableView, estimatedHeightForRowAt indexPath: IndexPath) -> CGFloat {
        return UITableViewAutomaticDimension
    }
    

    在这里你需要添加 top, bottom, right and left constraints

  • 1

    您应该只为 TOPBOTTOMHEIGHT 设置所有约束对于单元格视图/视图上的每个对象,如果有,则删除存在 middle Y 位置 . 因为你没有这个,把工件放在另一个视图上 .

  • 0

    对于目标c,这是我的一个很好的解决方案 . 它对我有用 .

    - (UITableViewCell *)tableView:(UITableView *)tableView cellForRowAtIndexPath:(NSIndexPath *)indexPath {
        cell.textLabel.text = [_nameArray objectAtIndex:indexPath.row];
        cell.textLabel.numberOfLines = 0;
        cell.textLabel.lineBreakMode = NSLineBreakByWordWrapping;
    }
    
    - (CGFloat)tableView:(UITableView *)tableView heightForRowAtIndexPath:(NSIndexPath *)indexPath {
        return UITableViewAutomaticDimension;
    }
    

    我们需要应用这两个更改 .

    1)cell.textLabel.numberOfLines = 0;
      cell.textLabel.lineBreakMode = NSLineBreakByWordWrapping;
    
    2)return UITableViewAutomaticDimension;
    
  • 1

    我最初也遇到过这个问题,我已经从这段代码中解决了我的问题,试着避免使用 self.tableView.reloadData() 代替这个代码来获得动态高度

    [self.tableView reloadSections:[NSIndexSet indexSetWithIndex:0] withRowAnimation:UITableViewRowAnimationFade];
    
  • 0

    除了别人说的话,

    设置与标签相关的标签限制!

    因此,不是将标签的约束放在其周围的其他事物上,而是将其约束到表格视图单元格的内容视图中 .

    然后,确保标签的高度设置为大于或等于0,并且行数设置为0 .

    然后在ViewDidLoad中添加:

    tableView.estimatedRowHeight = 695

    tableView.rowHeight = UITableViewAutomaticDimension

  • -1
    enter code here self.Itemtableview.estimatedRowHeight = 0;
                self.Itemtableview.estimatedSectionHeaderHeight = 0;
                self.Itemtableview.estimatedSectionFooterHeight = 0;
    
    
                [ self.Itemtableview reloadData];
                self.Itemtableview.frame = CGRectMake( self.Itemtableview.frame.origin.x,  self.Itemtableview.frame.origin.y,  self.Itemtableview.frame.size.width,self.Itemtableview.contentSize.height + self.Itemtableview.contentInset.bottom + self.Itemtableview.contentInset.top);
    
  • 0

    设置适当的约束并将委托方法更新为:

    func tableView(_ tableView: UITableView, heightForRowAt indexPath: IndexPath) -> CGFloat {
        return UITableViewAutomaticDimension
    }
    func tableView(_ tableView: UITableView, estimatedHeightForRowAt indexPath: IndexPath) -> CGFloat {
        return UITableViewAutomaticDimension
    }
    

    这将解决动态单元高度问题 . 如果不是你需要检查约束 .

相关问题