在iPhone中动态更改UITableviewCell高度? [英] Change UITableviewCell height dynamically in iPhone?

查看:147
本文介绍了在iPhone中动态更改UITableviewCell高度?的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我在UITableViewCell中有三个定制的UILabel。这样的单元格设计

 日期(12.1.2012)时间(12.00pm)
费用:$ 20.00
详细信息

我已经在 - (CGFloat)中指定了行高度100 tableView:(UITableView *)tableView heightForRowAtIndexPath:(NSIndexPath *)indexPath; 。但是,现在的细节有些时候从服务器返回空(Null)。那时候,我需要从单元格中删除详细信息标签,并将特定的单元格(行)高度更改为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:@Helveticasize: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:@Helveticasize: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:@Helveticasize: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:@Helveticasize: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 = @; //这里我不想显示此标签并将行高调整为(60);
}
else
{
eventLabel.text = description;
}
返回单元格;
}

我该怎么做?谢谢。

解决方案

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

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

此外,您需要删除 @Details cellForRowAtIndexPath中的相同if语句中:。事先标记它,从子视图中识别它,并将其从单元格中删除。


I have three custom UILabel in UITableViewCell. The cell design like this,

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

I have specified the row height 100 in - (CGFloat)tableView:(UITableView *)tableView heightForRowAtIndexPath:(NSIndexPath *)indexPath;. But, now the details some times return empty(Null) from the server. On that time i need to remove the Details label from the cell and change the particular cell(row) height to 70. How can i do this? I searched my level best in Google. But, i still just confused to do this. Can you please help me? Thanks. I got some idea from this link - http://www.cimgf.com/2009/09/23/uitableviewcell-dynamic-height/. They used only one label to resize the row height. Please help me.

- (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;
    }

How can i do this? Thanks.

解决方案

While returning the height,check if the detail description is empty. If it is return 60.

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

Also, you need to remove the label with @"Details" in the same if statement in cellForRowAtIndexPath:. Tag it beforehand, identify it from the subviews and remove it from the cell.

这篇关于在iPhone中动态更改UITableviewCell高度?的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆