【问题标题】:UITableViewAutomaticDimension applied on subtitle table view cell not working properly应用于字幕表视图单元格的 UITableViewAutomaticDimension 无法正常工作
【发布时间】:2016-08-16 12:50:07
【问题描述】:

我正在为列表创建一个视图。它在字幕中具有动态长度。所以我使用了 UITableViewAutomaticDimension 属性。我没有应用任何约束,因为两个标签都是表格视图单元格属性,所以不需要创建新的自定义单元格。

但是在运行这个项目之后,我标记了 UITableViewAutomaticDimension 不能正常工作。我不知道我做错了什么。我的代码如下所示

视图确实加载了

@IBOutlet weak var tblabc: UITableView!
    override func viewDidLoad() {
        super.viewDidLoad()

        tblabc.separatorColor = UIColor.clearColor()

        tblabc.estimatedRowHeight = 44
        tblabc.rowHeight = UITableViewAutomaticDimension



        // Do any additional setup after loading the view.
    }

表视图委托和数据源

func numberOfSectionsInTableView(tableView: UITableView) -> Int {
        return 1
    }
    func tableView(tableView: UITableView, numberOfRowsInSection section: Int) -> Int {
        return 5
    }
    func tableView(tableView: UITableView, heightForRowAtIndexPath indexPath: NSIndexPath) -> CGFloat {
        return UITableViewAutomaticDimension
    }
    func tableView(tableView: UITableView, cellForRowAtIndexPath indexPath: NSIndexPath) -> UITableViewCell
    {
        var cell = tableView.dequeueReusableCellWithIdentifier("Cell")
        if (cell == nil)
        {

            cell = UITableViewCell(style: .Subtitle, reuseIdentifier: "Cell")
            cell?.selectionStyle = .None
            cell?.detailTextLabel?.font = _SETREGULARFONT_KARLA(IS_IPAD ? 19:15)
            cell?.textLabel?.font = _SETBOLDFONT_KARLA(IS_IPAD ? 17:13)
            cell?.detailTextLabel?.textColor = getColorFromRGB(120, G: 132, B: 158, A: 1.0)
            cell?.detailTextLabel?.numberOfLines = 0
            cell?.detailTextLabel?.lineBreakMode = .ByTruncatingTail
            cell?.selectionStyle = .None

        }

        cell?.textLabel?.text = "Testing" + "\(indexPath.row)"
        cell?.detailTextLabel?.text = "tesfhjdsfskdfhjkfhdjskfhsjkdfhdjsxdfgfjljkgjfklgfhsdjfhjkdshfdjskfhdjfjkfhafyhdsifjhdjksfbshdjkfkhdksjfhdjsfyhds8ufdhsfjkhdsfjhdfudsiufhdsfh"
        cell?.imageView?.image = UIImage(named: "new_temp")

        return cell!
    }

查看如下图所示

【问题讨论】:

标签: ios swift uitableview


【解决方案1】:

当您有一个 多行 detailTextLabel 时,内置 UITableViewCellStyles(全部!)的 UITableViewAutomaticDimension 高度计算基本上是“损坏”的。正如 Venkat Reddy 评论的那样,请在此处查看我的解决方案:Autolayout ignores multi-line detailTextLabel when calculating UITableViewCell height (all styles)

[具有适当权限的人应考虑将此问题标记为重复问题]

【讨论】:

    【解决方案2】:

    检查此修改代码:-

    func numberOfSectionsInTableView(tableView: UITableView) -> Int {
        return 1
    }
    func tableView(tableView: UITableView, numberOfRowsInSection section: Int) -> Int {
        return 5
    }
    func tableView(tableView: UITableView, cellForRowAtIndexPath indexPath: NSIndexPath) -> UITableViewCell
    {
        var cell = tableView.dequeueReusableCellWithIdentifier("Cell")
        if (cell == nil)
        {
    
            cell = UITableViewCell(style: .Subtitle, reuseIdentifier: "Cell")
            cell?.selectionStyle = .None
            cell?.detailTextLabel?.font = _SETREGULARFONT_KARLA(IS_IPAD ? 19:15)
            cell?.textLabel?.font = _SETBOLDFONT_KARLA(IS_IPAD ? 17:13)
            cell?.detailTextLabel?.textColor = getColorFromRGB(120, G: 132, B: 158, A: 1.0)
            cell?.detailTextLabel?.numberOfLines = 0
            cell?.detailTextLabel?.lineBreakMode = .ByTruncatingTail
            cell?.selectionStyle = .None
    
        }
    
        cell?.textLabel?.text = "Testing" + "\(indexPath.row)"
        cell?.detailTextLabel?.text = "tesfhjdsfskdfhjkfhdjskfhsjkdfhdjsxdfgfjljkgjfklgfhsdjfhjkdshfdjskfhdjfjkfhafyhdsifjhdjksfbshdjkfkhdksjfhdjsfyhds8ufdhsfjkhdsfjhdfudsiufhdsfh"
        cell?.imageView?.image = UIImage(named: "new_temp")
    
        return cell!
    }
    

    请移除 HeightForRowAtIndex 数据源方法。

    【讨论】:

      【解决方案3】:

      这个问题应该在 iOS 11 中得到解决。在较低版本上运行良好的快捷解决方案是通过将其 textlabel.attributedText 设置为具有更粗的第一行 + \n 在基本单元格中伪造所需的字幕外观+ 常规更多行。

      【讨论】:

        【解决方案4】:

        试试这个代码:

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

        删除两行:

        tblabc.estimatedRowHeight = 44
        tblabc.rowHeight = UITableViewAutomaticDimension
        

        【讨论】:

        • 估计行高不应该是 UITableViewAutomaticDimension,它应该是一个实数,以帮助系统在自动布局确定动态行高之前计算行高的估计值。
        猜你喜欢
        • 2015-10-20
        • 1970-01-01
        • 1970-01-01
        • 1970-01-01
        • 2015-12-17
        • 2023-03-10
        • 2023-03-13
        • 1970-01-01
        • 1970-01-01
        相关资源
        最近更新 更多