ios 更改单个单元格 UITableView 的高度

声明:本页面是StackOverFlow热门问题的中英对照翻译,遵循CC BY-SA 4.0协议,如果您需要使用它,必须同样遵循CC BY-SA许可,注明原文地址和作者信息,同时你必须将它归于原作者(不是我):StackOverFlow 原文地址: http://stackoverflow.com/questions/31155795/
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-08-31 06:40:22  来源:igfitidea点击:

Changing height of individual cell UITableView

iosswiftuitableview

提问by Greg Peckory

I would like something like the following:

我想要以下内容:

func tableView(tableView: UITableView, cellForRowAtIndexPath indexPath: NSIndexPath) -> UITableViewCell {

    let cell = tableView.dequeueReusableCellWithIdentifier("NewsCell", forIndexPath: indexPath) as! UITableViewCell

    if indexPath == 3{

        cell.height = "50dp"

    }       

    return cell

}

what is the alternative or the easiest way to go about this?

解决此问题的替代方法或最简单的方法是什么?

EDIT

编辑

Is it possible for me to also specify section number: i.e-

我是否也可以指定部分编号:即-

if sectionIndex == 5

回答by Zhu Shengqi

I suggest use the heightForRowAtIndexPathfunction. TableView will call this function to determine the row height for a specific indexpath.

我建议使用heightForRowAtIndexPath函数。TableView 将调用此函数来确定特定索引路径的行高。

Sample code:

示例代码:

func tableView(tableView: UITableView, heightForRowAtIndexPath indexPath: NSIndexPath) -> CGFloat {
    if indexPath.section == SECTION_INDEX {
        return 60
    } else {
        return UITableViewAutomaticDimension
    }
}

Swift 4:

斯威夫特 4:

func tableView(_ tableView: UITableView, heightForRowAt indexPath: IndexPath) -> CGFloat {
    if indexPath.section == SECTION_INDEX {
        return 60
    } else {
        return UITableViewAutomaticDimension
    }
}

A little explain: indexPath have two properties: section and row. By checking these two properties, you can make your own control flow to determine the height for each cell.

稍微解释一下:indexPath 有两个属性:section 和 row。通过检查这两个属性,您可以制作自己的控制流来确定每个单元格的高度。

If you return 60, it means you want the cell's height to be 60 points. If you return UITableViewAutomaticDimension, you want the system to decide the best height for you (in this case, you'd better set autolayout for the cell in storyboard).

如果您返回 60,则表示您希望单元格的高度为 60 点。如果您返回 UITableViewAutomaticDimension,您希望系统为您决定最佳高度(在这种情况下,您最好为故事板中的单元格设置自动布局)。

I also suggest you take the stanford course CS193p on iTunes U, it would be of great help to you :)

我还建议你参加 iTunes U 上的 stanford course CS193p,这对你有很大帮助:)

回答by vadian

the easiest way is to override heightForRowAtIndexPath

最简单的方法是覆盖 heightForRowAtIndexPath

  override func tableView(tableView: UITableView, heightForRowAtIndexPath indexPath: NSIndexPath) -> CGFloat {
    let section = indexPath.section
    let row = indexPath.row
    if section == 0 && row == 2{
      return 50.0
    }
    return 22.0
  }

回答by Utsav Parikh

func tableView(tableView: UITableView, heightForRowAtIndexPath indexPath: NSIndexPath) -> CGFloat{
   if indexPath.section == 5
   {
       if indexPath.row == 3
       {
           return 150.0
       }
   }

   return 200.0
}