代码之家  ›  专栏  ›  技术社区  ›  PatriciaW

无法更改Xcode 6动态表中的行高度

  •  1
  • PatriciaW  · 技术社区  · 9 年前

    我有一个UItableViewController,并希望行高度适应UITextView中的文本。如果我在viewDidLoad中使用此代码,则会使行的高度太小,无法容纳文本:

     self.tableView.rowHeight = UITableViewAutomaticDimension   
     self.tableView.estimatedRowHeight = 50
    

    我唯一能找到的调整大小的方法是更改情节提要中的表视图行高度。这会更改表视图中的每个单元格,这不是我想要的。

    有人对我缺失的东西有什么建议吗?

    4 回复  |  直到 9 年前
        1
  •  4
  •   PatriciaW    9 年前

    这是我的工作代码:

    override func tableView(tableView: UITableView,
        heightForRowAtIndexPath indexPath: NSIndexPath) -> CGFloat {
            var text:String
            if indexPath.section > 0 {
                text = switchNameArray [indexPath.row]
            } else {
                text = viewDescription
            }
            return calculateHeight(text)
    }
    
    func calculateHeight (text:String) -> CGFloat {
        if UIDevice.currentDevice().userInterfaceIdiom == .Pad
        {
            return CGFloat(count(text)+50) // iPad
        } else {
            return CGFloat(count(text)+30) // iPhone
        }
    }
    
        2
  •  3
  •   whereisleo    9 年前

    有一种替代方法可以更改每行的高度。

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

    如果要使其变大或变小,还可以创建和If Else条件。只需返回一个数字

        3
  •  1
  •   Caleb    9 年前

    您可以尝试使用:

    func tableView(tableView: UITableView,
        heightForRowAtIndexPath indexPath: NSIndexPath) -> CGFloat {
    
            return 100 //Whatever fits your need for that cell
    }
    

    您可以使用tableView获取单元格。cellForRowAtIndexPath(indexPath),并根据需要设置高度。

        4
  •  0
  •   Christian Abella    9 年前

    如果您希望它适应内容的大小 获取当前tableView框架,然后调整高度 这取决于内容物的高度。

    var frame = CGRect(origin: tableView.frame.origin, size: tableView.frame.size)
    
    frame.size.height = tableView.contentSize.height
    tableView.frame = frame