iPhone
iPhone

Reputation: 4170

iPhone: How to increase the height of cell dynamically as per the string

I am using UITableView with header in my app and I want to add string in the cell of tableview. In the cell below the header I want to fill string data and as per the size of the string I want to increase the height of the cell

for better understanding I attach the file.

I have check the link1,link2 and link3

but I am not able to get answer which I want.

for better understanding I attach the image too.

enter image description here

label Instructions indicates my header and in next cell I want to add string.

then How can I increase the height of cell according to string length.

thanx in advance.

Upvotes: 0

Views: 1720

Answers (3)

jorik
jorik

Reputation: 655

Try this code for cell size.

- (CGFloat)tableView:(UITableView *)tableView heightForRowAtIndexPath:(NSIndexPath *)indexPath
{


    NSString *text =@"your String";

    CGSize constraint = CGSizeMake(CellContentWidth - (CellMargin * 2), 20000.0f);

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

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


    return height + (cellMargin* 2);

}

This code use for cell label Height.

    NSString *text = [arr objectAtIndex:indexPath.row];

    CGSize constraint = CGSizeMake(labelwidth , 20000.0f);

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

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

    cell.lbl3.frame=CGRectMake(X,Y ,W, height);

Upvotes: 0

Ilanchezhian
Ilanchezhian

Reputation: 17478

Implement the method

-(CGFloat)tableView:(UITableView *)tableView heightForRowAtIndexPath:(NSIndexPath *)indexPath {
   //return appropriate value based on IndexPath.row

  UIFont *font = textLabel.font; // use custom label's font
  NSString *myText = @"your text for row at index path";
  CGSize size = [myText sizeWithFont:font forWidth:textLabel.frame.size.width lineBreakMode:UILineBreakModeWordWrap];
  return (size.height+20); //10 pixels space above the textlabel and below 10 pixels space
}

as mentioned in the link you have referred.

Upvotes: 2

Shanti K
Shanti K

Reputation: 2918

Use the following delegate method:

-(CGFloat)tableView:(UITableView *)tableView heightForRowAtIndexPath:(NSIndexPath *)indexPath {
   //return appropriate value based on IndexPath.row
}

Within this Method, from the datasource aray, check the length of the string for tht particular row and return the height accordingly.

Upvotes: 1

Related Questions