【问题标题】:how to specify height of row?如何指定行高?
【发布时间】:2013-07-31 04:59:57
【问题描述】:

我想实现一个表格视图,它显示特定行的可扩展单元格,所以我创建了我的自定义表格单元格,如果将其 expandContent 设置如下:

- (UITableViewCell *)tableView:(UITableView *)tableView cellForRowAtIndexPath:(NSIndexPath *)indexPath
{
    static NSString *CellIdentifier = @"Cell";
    CustomCell *cell = [tableView dequeueReusableCellWithIdentifier:CellIdentifier];
    NSString *shouldExpand = [self.datasource objectAtIndex:indexPath.row];
    if([shouldExpand isEqualToString:@"expand"]){
        [cell setExpandContent:@"Expand"];
    }
    else{
        [cell setTitle:@"a line"];
    }
    return cell;
}

但是,为了告诉 tableview 行高,我需要实现以下代码:

- (CGFloat)tableView:(UITableView *)tableView heightForRowAtIndexPath:(NSIndexPath *)indexPath
{
    CustomCell *cell = [self tableView:tableView cellForRowAtIndexPath:indexPath];
    return [cell cellHeight];
}

问题是heightForRowAtIndexPath方法会调用tableView:cellForRowAtIndexPath:的1000次,如果我的数据源包含1000条数据,时间太长了。

如何解决问题?

【问题讨论】:

  • 那么就不要调用 cellForRowAtIndexPath 了,反正这样做很奇怪。改为查询您的数据源。
  • 您的单元格是否只有两种尺寸?是否所有未展开的单元格都具有相同的高度,并且所有展开的行都具有相同的高度?
  • 所有未展开的单元格高度相同,但并非所有展开的行高度都相同@rob mayoff
  • 你让用户同时展开多行吗?

标签: ios uitableview ios5


【解决方案1】:

不,你应该先找到单元格的大小,然后发送计算的高度,不要调用 tableView:cellForRowAtIndexPath:它会导致递归,首先计算并发送高度。例如

         //say suppose you are placing the string inside tableview cell then u need to   calculate cell for example

    NSString *string = @"hello world happy coding";
    CGSize maxSize = CGSizeMake(280, MAXFLOAT);//set max height
     CGSize cellSize = [self.str sizeWithFont:[UIFont systemFontOfSize:17]
                   constrainedToSize:maxSize
                   lineBreakMode:NSLineBreakByWordWrapping];//this will return correct height for text
    return cellSize.height +10; //finally u return your height


【讨论】:

    【解决方案2】:

    如何解决问题?

    如果你真的有 1000 行,你应该考虑使用动态行高,因为即使你想出了一个快速的方法来确定行高,表格仍然会 > 需要分别询问每行的高度。 (事实上​​,如果你真的有 1000 行,你应该重新考虑你的整个设计——这对于线性界面来说数据太多了。)

    如果您必须对大量行使用动态行高,您至少需要找到一种快速确定高度的方法,而无需创建整个单元格。也许您可以确定影响行高的因素,并提出一种非常简化的计算高度的方法。如果您不能这样做,那么计算每行的高度一次然后将结果与行数据一起保存可能是有意义的,这样您就不必再次计算它,直到数据发生变化。

    【讨论】:

      【解决方案3】:

      这是我用来动态设置 UITableViewCell 高度的代码:

      - (CGFloat)tableView:(UITableView *)tableView heightForRowAtIndexPath:(NSIndexPath *)indexPath {
          NSDictionary* dict = [branchesArray objectAtIndex:indexPath.row];
          NSString* address = [NSString stringWithFormat:@"%@,%@\n%@\n%@\n%@",[dict objectForKey:@"locality"],[dict objectForKey:@"city"],[dict objectForKey:@"address"],[dict objectForKey:@"contactNumber"], [dict objectForKey:@"contactEmail"]];
      
          CGSize constraint = CGSizeMake(220, MAXFLOAT);
      
         CGSize size = [address sizeWithFont:[UIFont fontWithName:@"Helvetica" size:14.0f] constrainedToSize:constraint lineBreakMode:NSLineBreakByWordWrapping];
      
         CGFloat height1 = MAX(size.height, 110.0f);
         return height1+20;
      }
      

      以及在 - (UITableViewCell *)tableView:(UITableView *)tableView cellForRowAtIndexPath:(NSIndexPath *)indexPath 中设置框架

      【讨论】:

        猜你喜欢
        • 1970-01-01
        • 1970-01-01
        • 2017-06-14
        • 2013-11-29
        • 1970-01-01
        • 2019-04-03
        • 1970-01-01
        • 1970-01-01
        • 1970-01-01
        相关资源
        最近更新 更多