gpt4 book ai didi

objective-c - 为自定义单元格设置 uitableviewcell 高度

转载 作者:行者123 更新时间:2023-12-01 17:34:01 25 4
gpt4 key购买 nike

我希望根据您在 Interface Builder 中设置自定义单元格的值动态设置单元格高度。这没有成功,所以我继续前进,在 Apples 文档中找到了这个方法,可以用来设置单元格高度 - TableView :heightForRowAtIndexPath:

但是我不确定我是否正确使用它,因为我的 tableviewcells 都没有调整它们的高度,它们都保持相同的标准尺寸。这是我在这里使用的代码。

- (UITableViewCell *)tableView:(UITableView *)tableView cellForRowAtIndexPath:(NSIndexPath *)indexPath
{
static NSString *CellIdentifier = @"Cell";
UITableViewCell *cell = [tableView dequeueReusableCellWithIdentifier:CellIdentifier];

// Set cell height
[self tableView:tableView heightForRowAtIndexPath:indexPath];

// Configure the cell using custom cell
[[NSBundle mainBundle] loadNibNamed:@"CustomcellA" owner:self options:nil];
cell = customcellA; // customcellA getters and setters are set up in .h

return cell;
}

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

任何帮助将不胜感激

最佳答案

假设第三个单元格是高度的两倍:

- (CGFloat)   tableView:(UITableView *)tableView
heightForRowAtIndexPath:(NSIndexPath *)indexPath
{
if(indexPath.row == 2)
return 88;
return 44;
}

这个方法会被tableView调用。

你不应该这样做:
[self tableView:tableView heightForRowAtIndexPath:indexPath];

如何确保正确单元格的 indexPath 被识别为其他大小取决于您自己。

一般规则:
如果你实现了委托(delegate)协议(protocol)的方法,你永远不会自己调用它。只有具有符合协议(protocol)的委托(delegate)的类的对象才会调用委托(delegate)方法。

关于objective-c - 为自定义单元格设置 uitableviewcell 高度,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/10627084/

25 4 0
Copyright 2021 - 2024 cfsdn All Rights Reserved 蜀ICP备2022000587号
广告合作:1813099741@qq.com 6ren.com