【问题标题】:How can I set UITableView cell height programmatically in Swift?如何在 Swift 中以编程方式设置 UITableView 单元格高度?
【发布时间】:2017-01-10 00:36:00
【问题描述】:

如何以编程方式设置视图高度?我有这个代码:

cell.viewMain.frame = CGRectMake(cell.viewMain.frame.origin.x, cell.viewMain.frame.origin.y, cell.viewMain.frame.size.width, 65.0)

但这不起作用。

更新:

func tableView(tableView: UITableView, cellForRowAtIndexPath indexPath: NSIndexPath) -> UITableViewCell {
    let cell = tableView.dequeueReusableCellWithIdentifier("updateCell", forIndexPath: indexPath) as! UpdateCell
    
    if self.data[indexPath.row] == "b" {
        tbUpdate.rowHeight = 85
        cell.viewMain.frame = CGRectMake(cell.viewMain.frame.origin.x, cell.viewMain.frame.origin.y, cell.viewMain.frame.size.width, 65.0)
    }else{
        tbUpdate.rowHeight = 220
        cell.viewMain.frame = CGRectMake(cell.viewMain.frame.origin.x, cell.viewMain.frame.origin.y, cell.viewMain.frame.size.width, 200.0)
    }
    
    return cell
}

【问题讨论】:

  • 你在哪里设置这个?用什么方法?
  • in tableView cellForRowAtIndexPath
  • 我想用 if 条件进行动态大小
  • 你能展示你运行这段代码的整个函数/方法吗?这样我们就能更好地帮助您
  • 好的,我有更新我的问题

标签: ios swift uitableview


【解决方案1】:

TableView 单元格的高度由表格视图设置。

你需要实现UITableViewDelegate tableView:heightForRowAtIndexPath:

【讨论】:

    【解决方案2】:

    首先,rowHeight 会更改表格中所有行的高度。如果您想要特定行的特定高度,请实现tableView:heightForRowAtIndexPath: 方法。首先删除代码中的 tbUpdate.rowHeight。

    【讨论】:

      【解决方案3】:
      override func tableView(_ tableView: UITableView, heightForRowAt indexPath: IndexPath) -> CGFloat {
          var cellHeight:CGFloat = CGFloat()
      
          if indexPath.row % 2 == 0 {
              cellHeight = 20
          }
          else if indexPath.row % 2 != 0 {
              cellHeight = 50
          }
          return cellHeight
      }
      

      【讨论】:

      • 一定要在 viewDidLoad 上添加 self.tableviewObj.delegate = self
      【解决方案4】:

      您可以尝试使用自动布局来创建高度约束,然后将约束连接到出口。然后为该约束设置常量,

      var y: Float
      if x==0{ //some condition
          y = 10
      }else if x==1{ //some condition
          y = 20
      }
      cell.viewMainHeightConstraint.constant = y 
      cell.view.layoutIfNeeded() 
      

      把它放在你的 cellForRowAtIndexPath 中

      编辑:我将问题误解为在 cellView 中要求另一个视图,如果是这样,那么委托方法 heightForRowAtIndexPath 确实是正确的,如上述答案中所述。

      一个例子:

      func tableView(tableView: UITableView, heightForRowAtIndexPath indexPath: NSIndexPath) -> CGFloat 
      {
          if x == 0{
              return 100.0 //Choose your custom row height
          } else {
              return 50
          }
      }
      

      【讨论】:

      • 我更新了代码以适用于 tableViewCells。你是说你想要一个动态大小的高度吗?还是所有单元格的高度都是固定的?
      • 我想用 if 条件进行动态大小
      【解决方案5】:
      func tableView(_ tableView: UITableView, heightForRowAt indexPath: IndexPath) -> CGFloat {
              var height:CGFloat = CGFloat()
              if indexPath.row == 0 {
                  height = 80
              }
              else if indexPath.row == 1 {
                  height = self.view.frame.size.height - 44 - 64 // 44 is a tab bar height and 64 is navigationbar height.
                  print(height)
              }
              return height
          }
      

      【讨论】:

        猜你喜欢
        • 1970-01-01
        • 2011-04-08
        • 2012-06-26
        • 1970-01-01
        • 1970-01-01
        • 1970-01-01
        • 1970-01-01
        • 1970-01-01
        • 2020-05-31
        相关资源
        最近更新 更多