如何动态计算自定义可展开/可折叠UITableViewCell的高度



我有一个有趣的场景。我有一些在单独的xib文件中设计的自定义UITableViewCells。其中一个具有UIImageView,它将加载大小不恒定的图像,因此这意味着UIImageView的高度必须灵活。该单元格还有一个包含一些UILabels的UIView,UIView的大小是恒定的,比如100。我想在didselectrowatindexpath事件上展开和折叠单元格。要折叠单元格,我必须隐藏带有一些标签的UIView。请在这方面指导我实现目标。另外,我的问题是"当单元格展开和折叠时,我如何计算行的高度。"谢谢

编辑:这是我尝试过的。但失败

- (CGFloat)tableView:(UITableView *)tableView heightForRowAtIndexPath:(NSIndexPath *)indexPath {
static DynamicTableViewCell *cell = nil;
static dispatch_once_t onceToken;
dispatch_once(&onceToken, ^{
cell = [self.tableView dequeueReusableCellWithIdentifier:CellIdentifier];
});

[self setUpCell:cell atIndexPath:indexPath];

CGFloat cellHeight = [self calculateHeightForConfiguredSizingCell:cell];
if(cellHeight >0) {
if(cell.isExpanded) {

return cellHeight;
}
else
{
return (cellHeight - detailViewHeight);      // cell.detailView.frame.size.height = 100
}
}
else {
return cellHeight;
}
}

首先,由于性能原因,您不应该对单元格的实例进行任何引用。

其次,你应该使用模型以正确的方式构建你的细胞。提供的代码根本没有显示模型存储的使用情况。

- (CGFloat)tableView:(UITableView *)tableView heightForRowAtIndexPath:(NSIndexPath *)indexPath {
// Say you have a array of models NSArray<YourModel *> *dataSource;
YourModel *model = dataSource[indexPath.row]; // hope you have only one section.
}

第三,这是使用任何架构(如MVVM、VIPER或MVC)的好方法,因为如果你没有架构,你的产品在未来的支持中可能会遇到问题。所以在MVVM的情况下,YourModel就像ViewModel。

若要定义动态高度单元的状态,请使用特性isExpanded。这是一个很好的观点,但它应该在另一个地方定义——YourModel。如果你能以正确的方式做到这一点,你就会知道没有细胞的细胞的状态。

@interface YourModel ...
@property BOOL isExpanded;
@end

确保您在didSelect:中正确更改状态

- (void)tableView:(UITableView *)tableView didSelectRowAtIndexPath:(NSIndexPath *)indexPath {
YourModel *model = dataSource[indexPath.row];
model.isExpanded = !model.isExpanded; // inverse works like switch
// Then relayout your row
[tableView beginUpdates];
// I commented next line because it might not be needed for you
// Test it please
// [tableView reloadRowsAtIndexPaths:@[indexPath] withRowAnimation:UITableViewRowAnimationNone];
[tableView endUpdates]; 
}

所以回到高度ForRow:

- (CGFloat)tableView:(UITableView *)tableView heightForRowAtIndexPath:(NSIndexPath *)indexPath {
// Say you have a array of models NSArray<YourModel *> *dataSource;
YourModel *model = dataSource[indexPath.row]; // hope you have only one section.
// It's better to extract next code to function or any class, but not in your YourModel.
CGSize imageSize = [model.image doSizeCalulation]; // Use your algorithm to calculate image size
CGSize shortText = [model.shortText sizeWithAttributes:@[/* font attributes */]];
// If the cell is expanded then we should calculate height of text else height is zero.
CGSize fullText = model.isExpanded ? [model.fullText sizeWithAttributes:@[/* font attributes */]]; : 0; 
// just sum
return imageSize.height + shortText.height + fullText.height;
}

实现这一点的另一种方法是使用UITableViewAutomaticDimension(只需在heightForRow方法中返回)。在这种情况下,您应该正确设置约束,并在运行时根据isExpanded属性更改fullText的高度约束的常量。

最新更新