【问题标题】:tableView cell height... how to customize it?tableView 单元格高度...如何自定义?
【发布时间】:2011-04-13 19:50:20
【问题描述】:

是否可以在分组表视图中仅修改一个单元格的高度?
我有一个包含 3 行和 2 行的 2 个部分的表格视图...我将更改第二部分的第二行的行高...

我该怎么做?
谢谢!

【问题讨论】:

    标签: iphone uitableview height


    【解决方案1】:

    你可以看看这个方法:

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

    在您的情况下,代码应如下所示:

    - (CGFloat)tableView:(UITableView *)tableView heightForRowAtIndexPath:(NSIndexPath *)indexPath
    {
       if (indexPath.section == 1 && indexPath.row == 1) {
          return SPECIAL_HEIGHT;
       }
       return NORMAL_HEIGHT;
    }
    

    您可以查找有关the method here的更多详细信息

    【讨论】:

    • 好的,谢谢!但我有一个问题......如果我输入一个数字,比如 return 60.0;效果很好,但是如果我键入一些代码以根据 UILabel 文本返回高度,则单元格背景不会出现...我在 cellForRowAtIndexPath 方法中添加了此代码:
    • else if (indexPath.section == 1 && indexPath.row == 1) { descrizioneLabel = [[UILabel alloc] initWithFrame:CGRectMake(10, 5, 285, 50)]; descrizioneLabel.font = [UIFont boldSystemFontOfSize:14.0]; descrizioneLabel.backgroundColor = [UIColor clearColor];描述标签.numberOfLines = 0; descrizioneLabel.text = 希望.描述; [cell.contentView addSubview:descrizioneLabel]; }
    • 这是你发布的方法:-(CGFloat)tableView:(UITableView *)tableView heightForRowAtIndexPath:(NSIndexPath *)indexPath { if (indexPath.section == 1 && indexPath.row == 1) { NSString *test = descrizioneLabel.text; UIFont *cellFont = [UIFont boldSystemFontOfSize:14.0]; CGSize constraintSize = CGSizeMake(280.0, MAXFLOAT); CGSize labelSize = [testo sizeWithFont:cellFont constrainedToSize:constraintSize lineBreakMode:UILineBreakModeWordWrap];返回标签大小.高度; } 返回 44.0; }
    • 我不确定,但这是我的猜测:方法heightForRow在方法cellForRow之前被调用,所以当你在heightForRow中调用descrizoneLabel时,它还没有构造,你能测试一下吗?我这里没有 XCode
    • 好的,现在我明白了......我该怎么做才能创建标签,然后调用 heightForRow 方法?谢谢;)
    【解决方案2】:

    iOS 8 及更高版本中,我们可以使用 Dynamic Table View Cell 高度。

    使用这个功能UITableviewCell从它的内容中获取它的高度,我们不需要写heightForRowAtIndexPath

    我在 viewDidLoad()

    中要做的所有事情
    tableView.estimatedRowHeight = 44.0;
    tableView.rowHeight = UITableViewAutomaticDimension;
    

    如果cell 包含uilabel。然后将约束应用于uilabel

    确保将 Label --Lines-- 更改为 0

    单元格会随着uilabel的内容自动增长:

    【讨论】:

      【解决方案3】:
      【解决方案4】:

      看看

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

      对于索引路径,只需检查哪个行和哪个部分并相应地调整高度

      【讨论】:

        猜你喜欢
        • 1970-01-01
        • 1970-01-01
        • 2019-05-23
        • 1970-01-01
        • 1970-01-01
        • 1970-01-01
        • 2017-11-14
        • 1970-01-01
        • 1970-01-01
        相关资源
        最近更新 更多