【问题标题】:How to set dynamic height of custom UITabelview cell in swit如何在swift中设置自定义UITableviewcell的动态高度
【发布时间】:2016-09-29 16:56:08
【问题描述】:

我有一个 UITableView,其中填充了由 Xib(自动布局)创建的自定义单元格。该单元格由多个标签图像等和根据我们从 api 获得的响应更改大小的大小组成。我一直在努力设置动态高度。我正在使用以下方法。

func createTabelView ()
    {
    let nib = UINib(nibName: "DBEventCell", bundle: nil)
        self.eventTabelVew.registerNib(nib, forCellReuseIdentifier: Constants.CELL_IDENTIFIER)

        self.eventTabelVew.estimatedRowHeight = 200
        self.eventTabelVew.rowHeight = UITableViewAutomaticDimension
        self.eventTabelVew.separatorStyle = .None //UITableViewCellSeparatorStyle.SingleLine;
        self.eventTabelVew.separatorColor = UIColor.clearColor();
        self.eventTabelVew.backgroundColor = UIColor.whiteColor();

        self.eventTabelVew.setNeedsLayout()
        self.eventTabelVew.layoutIfNeeded()
    }


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

        return UITableViewAutomaticDimension
    }

但这不起作用。请帮忙,因为我最近几天才被打动。 提前致谢

【问题讨论】:

    标签: iphone uitableview row-height


    【解决方案1】:

    我觉得你不需要这个功能

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

    您还应该检查您的单元格自动布局约束并调整 UI 组件的属性以使其随内容收缩或扩展,也请查看本教程

    https://www.appcoda.com/self-sizing-cells/

    【讨论】:

    • 如果删除了这个函数estimateHeightForRowAtIndexPath 问题还是一样,我也跟着“appcoda.com/self-sizing-cells”的教程但是没用
    猜你喜欢
    • 2013-12-20
    • 2011-06-04
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 2015-06-06
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    相关资源
    最近更新 更多