在 iPhone 上动态更改 UITableviewCell 高度?

5

我有一个UITableViewCell中含有三个自定义的UILabel。这个单元格的设计如下图所示:

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

我已经在- (CGFloat)tableView:(UITableView *)tableView heightForRowAtIndexPath:(NSIndexPath *)indexPath;方法中指定了行高为100。但是,有时从服务器返回的详细信息为空(Null)。这时,我需要从单元格中删除Details标签,并将特定单元格(行)的高度更改为70。我该怎么做?我已经在Google上尽力搜索了,但是还是不知道如何解决。您能帮助我吗?感谢。我从这个链接中获得了一些想法-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;
    }

我该怎么做呢?谢谢。

2个回答

8

返回高度时,检查详细描述是否为空。如果为空,则返回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"标签的标签。预先标记它,从子视图中识别它,并从单元格中删除它。

MadhavanRP先生,谢谢您的回答。我已经在我的代码中尝试了您的答案,并且还在cellForRowAtIndexPath:中检查了条件。第一次它运行得很好。当我滚动表格视图时,单元格大小已更改,描述标签的位置也已更改。这意味着所有行的描述标签均已删除。请问您能否帮助我解决这个问题。谢谢。 - Gopinath
@Gopinath 为什么不在有描述时将详细信息标签添加为单元格的子视图。您可能需要创建一个自定义类来解决滚动问题。请参考此问题的答案:http://stackoverflow.com/questions/8352530/changing-the-position-of-custom-uibutton-in-custom-uitableviewcell - MadhavanRP

0
您可以通过使用CGSize来检查返回字符串的大小,例如:
CGSize size = [detailStr sizeWithFont:[UIFont boldSystemFontOfSize:12.0f] constrainedToSize:CGSizeMake(190, 40) lineBreakMode:UILineBreakModeWordWrap];

你可以根据需要更改宽度、高度和字体。在计算尺寸之前,请确保 detailStr 不等于 nil。


Sasdnib先生,感谢您的回答。我已经编辑了我的问题,请检查一下。我刚才有些困惑,您能再帮我解答一下吗?谢谢。 - Gopinath

网页内容由stack overflow 提供, 点击上面的
可以查看英文原文,
原文链接