【问题标题】:Change UITableviewCell height dynamically in iPhone?在 iPhone 中动态更改 UITableviewCell 高度?
【发布时间】:2012-01-13 05:32:34
【问题描述】:

我在 UITableViewCell 中有三个自定义 UILabel。像这样的细胞设计,

Date(12.1.2012)  Time(12.00pm)
Cost: $20.00
Details

我在- (CGFloat)tableView:(UITableView *)tableView heightForRowAtIndexPath:(NSIndexPath *)indexPath; 中指定了行高100。但是,现在细节有时会从服务器返回空(Null)。那时我需要从单元格中删除详细信息标签并将特定的单元格(行)高度更改为 70。我该怎么做?我在谷歌搜索我的水平最好。但是,我仍然很困惑这样做。你能帮我么?谢谢。我从这个链接得到了一些想法 - http://www.cimgf.com/2009/09/23/uitableviewcell-dynamic-height/。他们只使用一个标签来调整行高。请帮帮我。

- (CGFloat)tableView:(UITableView *)tableView heightForRowAtIndexPath:(NSIndexPath *)indexPath
{ 
  return 90;
}

    - (UITableViewCell *)tableView:(UITableView *)tableView cellForRowAtIndexPath:(NSIndexPath *)indexPath 
    {
        static NSString *CellIdentifier = @"Cell";

        UITableViewCell *cell = [tableView dequeueReusableCellWithIdentifier:CellIdentifier];
        if (cell == nil) 
        {
            cell = [[[UITableViewCell alloc] initWithStyle:UITableViewCellStyleDefault reuseIdentifier:CellIdentifier] autorelease];


            UILabel *dateLabel = [[UILabel alloc] initWithFrame:CGRectMake(10, 0, 100, 30)];
            dateLabel.tag = 100;
            dateLabel.backgroundColor = [UIColor clearColor];
            dateLabel.font = [UIFont fontWithName:@"Helvetica" size:16];
            dateLabel.font = [UIFont boldSystemFontOfSize:16];
            dateLabel.highlightedTextColor = [UIColor whiteColor];
            [cell.contentView addSubview: dateLabel]; 

            UILabel *timeLabel = [[UILabel alloc] initWithFrame:CGRectMake(120, 0, 200, 25)];
            timeLabel.tag = 101;
            timeLabel.backgroundColor = [UIColor clearColor];
            timeLabel.font = [UIFont fontWithName:@"Helvetica" size:14];
            timeLabel.highlightedTextColor = [UIColor whiteColor];
            [cell.contentView addSubview: timeLabel]; 

            UILabel *costLabel = [[UILabel alloc] initWithFrame:CGRectMake(10, 30, 300, 25)];
            costLabel.tag = 102;
            costLabel.backgroundColor = [UIColor clearColor];
            costLabel.font = [UIFont fontWithName:@"Helvetica" size:14];
            costLabel.font = [UIFont boldSystemFontOfSize:14];
            costLabel.highlightedTextColor = [UIColor whiteColor];
            [cell.contentView addSubview: costLabel];

            UILabel *eventLabel = [[UILabel alloc] initWithFrame:CGRectMake(10, 60, 300, 25)];
            eventLabel.tag = 103;
            eventLabel.backgroundColor = [UIColor clearColor];
            eventLabel.font = [UIFont fontWithName:@"Helvetica" size:14];
            eventLabel.font = [UIFont boldSystemFontOfSize:14];
            eventLabel.highlightedTextColor = [UIColor whiteColor];
            [cell.contentView addSubview: eventLabel];
         }

        UILabel *dateLabel = (UILabel *) [cell.contentView viewWithTag:100];
        dateLabel.text = [DateArray objectAtIndex:indexPath.row];

        UILabel * timeLabel = (UILabel *) [cell.contentView viewWithTag:101];
        timeLabel.text = [timeArray objectAtIndex:indexPath.row];

        UILabel * costLabel = (UILabel *) [cell.contentView viewWithTag:102];
        costLabel.text = [costArray objectAtIndex:indexPath.row];

        UILabel *eventLabel = (UILabel *) [cell.contentView viewWithTag:103];
        NSString *description = [eventArray objectAtIndex:indexPath.row];
        if([description isEqualToString:@""])
       {
         eventLabel.text = @""; // Here i don't want to show this label and resize(reduce) the row height to 60;
       }
       else
       {
         eventLabel.text = description;
       }
        return cell;
    }

我该怎么做?谢谢。

【问题讨论】:

    标签: ios dynamic uitableview row-height


    【解决方案1】:

    返回高度时,检查详细描述是否为空。如果是返回60。

    - (CGFloat)tableView:(UITableView *)tableView heightForRowAtIndexPath:(NSIndexPath *)indexPath
    { 
    NSString *description = [eventArray objectAtIndex:indexPath.row];
    if([description isEqualToString:@""])
        return 60
    else
    {
        return 90;
    }
    }
    

    此外,您需要在cellForRowAtIndexPath: 的同一 if 语句中删除带有@"Details" 的标签。预先标记它,从子视图中识别它并将其从单元格中删除。

    【讨论】:

    • Mr.MadhavanRP 感谢您的回答。我已经在我的代码中尝试了你的答案。我也检查了 cellForRowAtIndexPath: 中的条件。第一次它工作正常。当我滚动表格视图时,单元格大小已更改,并且描述标签已更改位置。这意味着描述标签已删除所有行。你能帮我么。谢谢
    • @Gopinath 为什么不尝试将详细信息标签添加为单元格的子视图,仅当您有一些描述时。您可能需要创建一个自定义类来解决滚动问题。参考这个问题的答案stackoverflow.com/questions/8352530/…
    【解决方案2】:

    您可以像这样使用CGSize 检查返回字符串的大小 -

    CGSize size = [detailStr sizeWithFont:[UIFont boldSystemFontOfSize:12.0f] constrainedToSize:CGSizeMake(190, 40) lineBreakMode:UILineBreakModeWordWrap];
    

    您可以根据自己的情况更改宽度、高度和字体。另外请在计算 size 之前检查 detailStr 是否不等于 nil。

    【讨论】:

    • Sasdnib 先生感谢您的回答。我已经编辑了我的问题。请检查这个。我只是混淆了你的答案,请你帮帮我。谢谢。
    猜你喜欢
    • 1970-01-01
    • 2014-05-19
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 2017-05-21
    • 1970-01-01
    • 2016-07-27
    • 1970-01-01
    相关资源
    最近更新 更多