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

标签 objective-c ios cocoa-touch uitableview

我希望根据您在 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/

相关文章:

ios - uitableview中scrollviewdidscroll方法的调用?

ios - SpriteKit - 返回到与离开时相同的持久场景。

objective-c - 更改视频 iPad/iPhone 的背景颜色

ios - 为什么 NSUserDefaults 在我的应用和共享扩展之间不起作用?

iphone - 保存从互联网下载的数据

iphone - 在 ios7 上,<input type=date> 在没有默认值的情况下不会显示在 webview 中

iphone - UIButton标题与图片对齐查询

objective-c - 在 HTTP Post 中实现超时

ios - 在锁屏 iOS 上显示交互式本地推送通知

iphone - 如何在 iPhone 应用程序中创建折线图?