0

I have been populating a UITable view cell with the data fetched from a database. The length of the data varies for different cells. So i need to expand or Contract the height depend the data length. Presently i am using a custom cell with the following code.

-(UITableViewCell *)tableView:(UITableView *)tableView cellForRowAtIndexPath:(NSIndexPath *)indexPath {

    static NSString *cellID= @"customcell4fan";
    customcell4fan *cell = (customcell4fan *)[tableView dequeueReusableCellWithIdentifier:cellID];

    if(cell==nil)
    {
        NSArray *nibObjects = [[NSBundle mainBundle] loadNibNamed:@"customcell4fan" owner:nil options:nil];

        for(id currentObject in nibObjects)
        {
            if([currentObject isKindOfClass: [customcell4fan class]])
            {
                cell = (customcell4fan *)currentObject;
            }

        }
    }

eleme = [xmlElementObjects objectAtIndex:indexPath.section];

NSString *email= [eleme.title stringByTrimmingCharactersInSet:[NSCharacterSet characterSetWithCharactersInString:@" \n\t"]];
    NSString *postData= [eleme.description stringByTrimmingCharactersInSet:[NSCharacterSet characterSetWithCharactersInString:@" \n\t"]];
cell.nameLabel.text=email;
    cell.postLabel.text=postData;

  return cell;

}
Hiren
  • 12,720
  • 7
  • 52
  • 72
  • 3
    Checkout this link:-http://stackoverflow.com/questions/9796947/how-can-i-resize-uitableviewcell-for-how-much-text-that-i-have/9798654#9798654 – Gypsa Mar 23 '12 at 05:24

4 Answers4

1

Calculate your cell's height and return in this method:

- (CGFloat)tableView:(UITableView *)tableView heightForRowAtIndexPath:(NSIndexPath *)indexPath;
Dinesh Raja
  • 8,501
  • 5
  • 42
  • 81
yibuyiqu
  • 728
  • 1
  • 7
  • 20
1

try this http://www.roostersoftstudios.com/2011/04/14/iphone-uitableview-with-animated-expanding-cells/ i find it very helpful

Bala
  • 2,895
  • 1
  • 19
  • 60
0

Calculate the size of the string ,According to assign the height of the cell.

To calculate the height of label use the following code,

CGSize stringSize = [urResponseStr sizeWithFont:[UIFont boldSystemFontOfSize:15]
                      constrainedToSize:CGSizeMake(320, 9999)
                          lineBreakMode:UILineBreakModeWordWrap];//stringSize.height is your label height

after calculating height of your two labels assign respective value to the individual cells in the following delegate.

 -(CGFloat)tableView:(UITableView *)tableView heightForRowAtIndexPath:(NSIndexPath *)indexPath{
    }
EXC_BAD_ACCESS
  • 2,699
  • 2
  • 21
  • 25
0

try with this answer

#define FONT_SIZE 14.0f
#define CELL_CONTENT_WIDTH 320.0f
#define CELL_CONTENT_MARGIN 10.0f

- (CGFloat)tableView:(UITableView *)tableView heightForRowAtIndexPath:(NSIndexPath *)indexPath;
{
  NSString *text = [items objectAtIndex:[indexPath row]];

  CGSize constraint = CGSizeMake(CELL_CONTENT_WIDTH - (CELL_CONTENT_MARGIN * 2), 20000.0f);

  CGSize size = [text sizeWithFont:[UIFont systemFontOfSize:FONT_SIZE] constrainedToSize:constraint lineBreakMode:UILineBreakModeWordWrap];

  CGFloat height = MAX(size.height, 44.0f);

  return height + (CELL_CONTENT_MARGIN * 2);
}

- (UITableViewCell *)tableView:(UITableView *)tv cellForRowAtIndexPath:(NSIndexPath *)indexPath
{
  UITableViewCell *cell;
  UILabel *label = nil;

  cell = [tv dequeueReusableCellWithIdentifier:@"Cell"];
  if (cell == nil)
  {
    cell = [[[UITableViewCell alloc] initWithFrame:CGRectZero reuseIdentifier:@"Cell"] autorelease];

    label = [[UILabel alloc] initWithFrame:CGRectZero];
    [label setLineBreakMode:UILineBreakModeWordWrap];
    [label setMinimumFontSize:FONT_SIZE];
    [label setNumberOfLines:0];
    [label setFont:[UIFont systemFontOfSize:FONT_SIZE]];
    [label setTag:1];

    [[label layer] setBorderWidth:2.0f];

    [[cell contentView] addSubview:label];

  }
  NSString *text = [items objectAtIndex:[indexPath row]];

  CGSize constraint = CGSizeMake(CELL_CONTENT_WIDTH - (CELL_CONTENT_MARGIN * 2), 20000.0f);

  CGSize size = [text sizeWithFont:[UIFont systemFontOfSize:FONT_SIZE] constrainedToSize:constraint lineBreakMode:UILineBreakModeWordWrap];

  if (!label)
    label = (UILabel*)[cell viewWithTag:1];

  [label setText:text];
  [label setFrame:CGRectMake(CELL_CONTENT_MARGIN, CELL_CONTENT_MARGIN, CELL_CONTENT_WIDTH - (CELL_CONTENT_MARGIN * 2), MAX(size.height, 44.0f))];

  return cell;
}
Hiren
  • 12,720
  • 7
  • 52
  • 72