0

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.

Community
  • 1
  • 1
iPhone
  • 4,092
  • 3
  • 34
  • 58

3 Answers3

2

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.

Ilanchezhian
  • 17,426
  • 1
  • 53
  • 55
  • I am assigning cell value using `cell.textLabel.text`. Do I need anything else ? – iPhone Dec 29 '11 at 09:26
  • In `cellForRowAtIndexPath`, you assign the text thru `cell.textLabel.text` as it is. No need to change. I have edited my answer. Pl. see – Ilanchezhian Dec 29 '11 at 09:38
  • `UITableViewCell *cell = [tableView cellForRowAtIndexPath:indexPath];` I got error msg EXC BAD ACCESS at this line. – iPhone Dec 29 '11 at 10:14
1

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.

Shanti K
  • 2,873
  • 1
  • 16
  • 31
0

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);
Sumit Mundra
  • 3,891
  • 16
  • 29
jorik
  • 645
  • 7
  • 15