【问题标题】:dynamic uitableview cell height动态uitableview单元格高度
【发布时间】:2011-11-06 21:29:43
【问题描述】:

我从 json 服务器异步获取数据,并将它们放在我的表格视图单元格中。我试图根据文本内容动态地制作单元格高度。我在互联网上找到了它,但到处都给出了相同的代码,使用 -

(CGFloat)tableView:(UITableView *)tableView heightForRowAtIndexPath:(NSIndexPath *)indexPath

我不知道如何实现。请指导我。

【问题讨论】:

    标签: ios xcode4 uitableview


    【解决方案1】:

    在此方法中,计算每行中使用的图像、文本的高度,并将高度作为浮点值返回。

    例如:

    -(CGFloat)tableView:(UITableView *)tableView heightForRowAtIndexPath:(NSIndexPath *)indexPath {
    
    float totalHeight = 0.0;
    
    BOOL ImageToBeDisplayed = [[yourDataArray objectAtIndex:indexPath.row]objectForKey@"yourImageKey"];
    
    if(ImageToBeDisplayed)
    
    totalHeight = 100 or some value of your choice
    }
    else{
    //Do nothing
    }
    
    If your row has string data as well.
    
    int length = [[[yourDataArray objectAtIndex:indexPath.row]objectForKey@"yourTextKey"] length];
    if(length<40)
    totalHeight + = 50;
    else if (length<80 && length>40)
    totalHeight + = 100;
    and so on
    
    and
    
    
    return totalHeight;
    
    }
    

    【讨论】:

    • 为什么会有硬编码常量?如果字体大小发生变化怎么办?
    猜你喜欢
    • 1970-01-01
    • 1970-01-01
    • 2019-08-15
    • 2011-11-27
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 2015-08-10
    • 1970-01-01
    相关资源
    最近更新 更多