-2

I am having one cell like Facebook wall post.

my cell is looking like the below image.

enter image description here

I want to resize my cell and text label on basis of text receive from WebService.

I have tried many solution from internet but no luck.

Code i have tried

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

 CGSize maximumLabelSize = CGSizeMake(9999, FLT_MAX);

 CGSize expectedLabelSize = [label sizeWithFont:[UIFont fontWithName:@"Verdana" size:10] constrainedToSize:maximumLabelSize lineBreakMode:NSLineBreakByWordWrapping];


 [cellNewsimg.lblStatus sizeThatFits:expectedLabelSize];
cellNewsimg.lblStatus.frame.size.height=expectedLabelSize.height;
objtableviewNewsFeedback.rowHeight = UITableViewAutomaticDimension;
objtableviewNewsFeedback.estimatedRowHeight = 500;


cellNewsimg.lblStatus.frame.size expectedLabelSize;
return 350.0+200;

}

Cell for row at indexpath

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


NSArray *ary=[[NSBundle mainBundle] loadNibNamed:@"Newsfeed" owner:self options:nil];

cellNewsimg=(Newsfeed *)[ary firstObject];
cellNewsimg.lblStatus.text=label;

cellNewsimg.lblStatus.lineBreakMode =UILineBreakModeWordWrap;
cellNewsimg.lblStatus.numberOfLines = 0;
[cellNewsimg.lblStatus sizeThatFits:expectedLabelSize];
return cellNewsimg;
}
NSNoob
  • 5,548
  • 6
  • 41
  • 54

1 Answers1

0

Try this:

- (CGFloat)tableView:(UITableView )tableView heightForRowAtIndexPath:(NSIndexPath )indexPath  
{ 
  return [_tbl.dataSource tableView:tableView cellForRowAtIndexPath:indexPath].contentView.frame.size.height + 8;
}


- (UITableViewCell )tableView:(UITableView )tableView cellForRowAtIndexPath:(NSIndexPath *)indexPath
{
 NSArray *ary=[[NSBundle mainBundle] loadNibNamed:@"Newsfeed" owner:self options:nil];
 cellNewsimg=(Newsfeed *)[ary firstObject];

 cellNewsimg.lblStatus.frame = CGRectMake(0,0,ScreenWidth,21); //set your x,y and Screenwidth is a float, set it dynamically as per your view

 cellNewsimg.lblStatus.text=label;

 cellNewsimg.lblStatus.lineBreakMode = NSLineBreakByWordWrapping;
 cellNewsimg.lblStatus.numberOfLines = 0;
 [cellNewsimg.lblStatus sizeToFit];  
 cell.contentView.frame = CGRectMake(0, 0, _tbl.frame.size.width, CGRectGetMaxY(cell.lblStatus.frame) + 8) ;

 return cellNewsimg;
}  

Just get the label size in cellForRowAtIndexPath:indexPath and set all controls according to that. Don't calculate anything in heightForRowAtIndexPath.

Hope it helps.

Milan Gupta
  • 1,181
  • 8
  • 21
  • Have you set Autoresizing for label from storyboard? If yes, then remove it. Also as per your view, you will have to set `UIImage` y after setting label's frame and all the views below it. And take `CGRectGetMaxY ` of the last control in your cell. – Milan Gupta Jan 04 '17 at 08:42
  • You need to use Autolayout, see here: http://stackoverflow.com/questions/18746929/using-auto-layout-in-uitableview-for-dynamic-cell-layouts-variable-row-heights – koen Jan 04 '17 at 13:30