【问题标题】:dynamic cell height in ios using constrain programatically in swiftios中的动态单元格高度在swift中以编程方式使用约束
【发布时间】:2015-01-16 07:23:02
【问题描述】:

我正在快速开发一个项目,该项目需要根据标签内容使单元格大小动态变化,因此我搜索了动态单元格高度,我找到了一些解决方案,但所有这些都包括使用情节提要来分配约束。没有故事板是否可以做到这一点。是否可以以编程方式进行?我的意思是在没有故事板的情况下应用自我调整大小

我的代码是:

class a: UITableViewDelegate, UITableViewDataSource {
    tableView = UITableView(frame: CGRectMake(0, 0, self.view.frame.width, self.view.frame.height), style: UITableViewStyle.Plain)
    tableView.delegate = self
    tableView.dataSource = self
    tableView.estimatedRowHeight = 100
    tableView.rowHeight = UITableViewAutomaticDimension
    self.view.addSubview(tableView)
}

希望问题很清楚。

提前致谢。

编辑:下面的答案都没有帮助

【问题讨论】:

标签: ios uitableview swift nslayoutconstraint uitableviewautomaticdimension


【解决方案1】:

试试这个方法。

 override func tableView(tableView: UITableView!, heightForRowAtIndexPath indexPath: NSIndexPath!) -> CGFloat {
 return // get cell text label high and return +10 or +20 height. what is label height..
}

【讨论】:

  • 我想使用自动调整大小的单元格,你能告诉我吗?
【解决方案2】:
【解决方案3】:

你可以试试这个方法。

func tableView(tableView: UITableView, estimatedHeightForRowAtIndexPath indexPath: NSIndexPath) -> CGFloat
{
 self.dashBoardTableView.rowHeight = UITableViewAutomaticDimension

    if (CellIdentifier == "dashBoardTableCellID")
    {
       dashBoardTableView.rowHeight =  96
    }
    else
    {
        dashBoardTableView.rowHeight =  60
    }

    return self.dashBoardTableView.rowHeight
}

【讨论】:

    【解决方案4】:

    是的,你可以,你可以在代码中创建你的约束。 我建议你看看 NSLayoutConstraint 类:

    NSLayoutConstraint Apple documentation

    【讨论】:

      【解决方案5】:

      iOS 8 支持自定义大小的表格视图单元格。要启用它,首先创建具有正确约束的单元格。最终使用代码如下

      tableView.estimatedRowHeight = 50
      tableView.cellHeight = UITableViewCellAutomaticDimension
      

      estimatedRowHeight 只是 tableview 的提示。当 tableview 重用单元格时,它会查看其内容并根据它调整大小。

      【讨论】:

      • 我知道,我想知道我该怎么做我的意思是代码
      猜你喜欢
      • 1970-01-01
      • 2020-05-31
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      • 2015-04-02
      • 1970-01-01
      • 2014-06-18
      • 2016-09-28
      相关资源
      最近更新 更多