Change UITableviewCell height dynamically in iPhone?
Asked Answered
R

2

5

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.

Rooky answered 13/1, 2012 at 5:32 Comment(0)
R
8

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.

Restaurant answered 13/1, 2012 at 6:8 Comment(2)
Mr.MadhavanRP thanks for your answer. I have tried your answer in my code. I have checked the condition in cellForRowAtIndexPath: also. First time it is working fine. When i scroll the tableview, the cell size has changed and the description label has changed the location. It means the description label has remove for all rows. Can you please help me. thanksRooky
@Rooky why don't you try adding the details label as cell's subview only when you have some description. You might need to create a custom class to solve the scrolling issue. refer to this question's answer #8353030Restaurant
J
0

You can check size of return string by using CGSize like this -

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

you can change width, height and font according to your condition. Also please check if detailStr is not equal to nil before calculating size.

Jan answered 13/1, 2012 at 5:40 Comment(1)
Mr.Sasdnib thanks for your answer. I have edited my question. Please check this. I just confused your answer can you please help me. Thanks.Rooky

© 2022 - 2024 — McMap. All rights reserved.