繁体   English   中英

设置自定义单元的uitableviewcell高度

[英]setting uitableviewcell height for custom cell

我一直在根据您在Interface Builder中设置自定义单元格的值来动态设置单元格高度。 这没有解决,所以我继续前进,并在Apple文档中找到了可用于设置像元高度的方法-tableView:heightForRowAtIndexPath:

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

- (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取决于您。

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

heightForRowAtIndexPath将被自动调用,您需要返回所需的自定义高度。 因此,您需要返回基于数据或基于高度的任何内容的动态高度,而不是返回44。

暂无
暂无

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM