如何将UITableView静态单元格设置为自定义高度?

use*_*180 9 xcode row-height uitableview ios

我有一个带有静态单元格的UITableView,我正在尝试在满足条件时更改单元格的高度.条件正在满足,但细胞高度不会改变.

我正在使用的代码是

[self.tableView setRowHeight:10];
Run Code Online (Sandbox Code Playgroud)

self.tableView rowHeight = 10;
Run Code Online (Sandbox Code Playgroud)

这些都不会改变单元格的行高是否还有另一种方法可以改变它们?

Jul*_*per 18

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

- (CGFloat)tableView:(UITableView *)tableView heightForRowAtIndexPath:(NSIndexPath *)indexPath
{
    return customTableCellHeight;
}
Run Code Online (Sandbox Code Playgroud)

然后更改customTableCellHeight的值:

if (condition) {
    customTableCellHeight = 80; // or whatever
}
Run Code Online (Sandbox Code Playgroud)

当您的表格单元格高度需要更改时(如条件更改时,您需要调用

 [self.tableView reloadData]; //This calls heightForRowAtIndexPath for all cells
Run Code Online (Sandbox Code Playgroud)

  • 它不需要`reloadData`调用. (2认同)
  • 您需要在heightForRowAtIndexPath内部调用reloadData,但在*condition*更改时,需要在其外部调用reloadData.这将导致重新计算高度并允许动态调整细胞大小. (2认同)