【问题标题】:How to set a UITableView static cell to a custom height?如何将 UITableView 静态单元格设置为自定义高度?
【发布时间】:2012-06-23 20:42:39
【问题描述】:

我有一个带有静态单元格的 UITableView,我试图在满足条件时更改单元格的高度。条件得到满足,但单元格高度不会改变。

我使用的代码是

[self.tableView setRowHeight:10];

和

self.tableView rowHeight = 10;

这些都不会改变单元格的行高 有没有其他方法可以改变它们?

【问题讨论】:

标签: ios xcode uitableview row-height


【解决方案1】:

您需要在 tableview 的 .m 中实现以下委托方法:

- (CGFloat)tableView:(UITableView *)tableView heightForRowAtIndexPath:(NSIndexPath *)indexPath
{
    return customTableCellHeight;
}

然后改变customTableCellHeight的值:

if (condition) {
    customTableCellHeight = 80; // or whatever
}

当你的表格单元格高度需要改变时(比如当条件改变时,你需要调用

 [self.tableView reloadData]; //This calls heightForRowAtIndexPath for all cells

【讨论】:

  • 它不需要reloadData 调用。
  • 当条件改变时,你需要不在heightForRowAtIndexPath内部调用reloadData,而是在它外部调用。这将导致重新计算高度并允许动态调整单元格大小。
【解决方案2】:

你可以将它用于tableview的所有行

override func tableView(tableView: UITableView, estimatedHeightForRowAtIndexPath indexPath: NSIndexPath) -> CGFloat {
      return 100
    }

【讨论】:

    【解决方案3】:

    您可以创建具有文本高度的动态单元格,然后您可以在表格视图中设置静态和动态单元格

    这里是带有文本的动态单元格的链接 How to change cell height dynamically in UITableView static cell

    【讨论】:

      猜你喜欢
      • 2011-11-27
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      • 2017-08-09
      • 2015-06-04
      • 1970-01-01
      • 2013-12-03
      • 1970-01-01
      相关资源
      最近更新 更多