Swift/XCode 6.4:如何以编程方式更改表格视图控制器中单元格的高度

声明:本页面是StackOverFlow热门问题的中英对照翻译,遵循CC BY-SA 4.0协议,如果您需要使用它,必须同样遵循CC BY-SA许可,注明原文地址和作者信息,同时你必须将它归于原作者(不是我):StackOverFlow 原文地址: http://stackoverflow.com/questions/32403172/
Warning: these are provided under cc-by-sa 4.0 license. You are free to use/share it, But you must attribute it to the original authors (not me): StackOverFlow

提示:将鼠标放在中文语句上可以显示对应的英文。显示中英文
时间:2020-09-15 07:46:46  来源:igfitidea点击:

Swift/XCode 6.4: how to programmatically change the height of cells in table view controller

iosxcodeswiftuitableview

提问by SagittariusA

In my iOS app there is a table view controller of static cells containing three different types of cells whose style is Subtitle. One of these should be much higher than the other two because it is supposed to contain a very long text. So, in its size inspector I set an height of 100 but when I create a new cell in the code through its identifier, then the height is always the default one.

在我的 iOS 应用程序中有一个静态单元格的表视图控制器,其中包含三种不同类型的单元格,其样式为副标题。其中一个应该比其他两个高得多,因为它应该包含很长的文本。因此,在其大小检查器中,我将高度设置为 100,但是当我通过其标识符在代码中创建一个新单元格时,高度始终为默认值。

How can I change it? I've read that I should override this method:

我怎样才能改变它?我读过我应该覆盖这个方法:

override func tableView(tableView: UITableView,
    heightForRowAtIndexPath indexPath: NSIndexPath) -> CGFloat {

        return 100 //Whatever fits your need for that cell
}

but then I don't know how to use it. Can you explain please? enter image description here

但后来我不知道如何使用它。你能解释一下吗? 在此处输入图片说明

回答by Rajat

As you said that you tableviewcells' are statics, so you know that which cell is having more height, pass the indexpathof that cell in heightForRowAtIndexPath and return the expected height of cell from there. Suppose your 1st cell is having more height, then use the heightForRowAtIndexPathlike this

正如您所说,您的tableview单元格是静态的,因此您知道哪个单元格的高度更高,请indexpath在 heightForRowAtIndexPath 中传递该单元格的 ,并从那里返回单元格的预期高度。假设你的第一个单元格有更高的高度,然后使用heightForRowAtIndexPath这样的

 func tableView(tableView: UITableView,
    heightForRowAtIndexPath indexPath: NSIndexPath) -> CGFloat {
        if indexPath.row == 1 {
            return 100 //Whatever fits your need for that cell
        } else {
            return 50 // other cell height
        }
}

No need to create three different prototype cell, just create on cell that will be sufficient.

无需创建三个不同的原型单元格,只需在单元格上创建就足够了。

回答by vadian

You could implement heightForRowAtIndexPathas you mentioned in you post and get the cell with cellForRowAtIndexPath. Then set the height depending on the reuseIdentifier

您可以heightForRowAtIndexPath按照您在帖子中提到的方式实施并使用cellForRowAtIndexPath. 然后根据需要设置高度reuseIdentifier

override func tableView(tableView: UITableView, heightForRowAtIndexPath indexPath: NSIndexPath) -> CGFloat {
  if let cell = tableView.cellForRowAtIndexPath(indexPath) {
    if cell.reuseIdentifier == "SpecialCell" {
      return 100.0
    }
  }
  return 44.0
}

回答by Glenn

Why don't you use 2 different TableViewCells in Interface builder and choose the right one to use in cellForRowAtIndexPath ?

为什么不在 Interface builder 中使用 2 个不同的 TableViewCells 并选择正确的一个在 cellForRowAtIndexPath 中使用?

That's the easiest way and you can even differentiate more between the 2 types when it's necessary.

这是最简单的方法,您甚至可以在必要时更多地区分这两种类型。