UIlabel shrinks when scrolling UiTableView

I have two UILabel aside from UICell that contains dynamic text, so I need to resize its frame to fit the content for which im uses the [string sizeWithFont] method to calculate the height of the label view frame inside the TableView heightForRowAtIndexPath to set the cell height according to the height of the inscription. Now the problem is when I look at the table, the label inside the cell starts to decrease if I remove the [label sizeToFit] method, which it does not compress, but because my labels overlap, which looks very dirty. Where am I wrong, please help me ..

here is my code for cellRowAtIndexPath method

 - (UITableViewCell *)tableView:(UITableView *)tableView cellForRowAtIndexPath:(NSIndexPath *)indexPath { NSLog(@"cell"); BOOL ente = FALSE; static NSString *CellIdentifier = @"CustomCell"; CustomCell *cell = (CustomCell *) [tableView dequeueReusableCellWithIdentifier:CellIdentifier]; if (cell == nil) { NSArray *topLevelObjects = [[NSBundle mainBundle] loadNibNamed:@"CustomCell" owner:nil options:nil]; for (id currentObject in topLevelObjects){ if ([currentObject isKindOfClass:[UITableViewCell class]]){ cell = (CustomCell *) currentObject; ente = TRUE; break; } } } /* [cell.title sizeToFit]; [cell.dataTime sizeToFit]; CGSize size1 = [[mainEventArray objectAtIndex:[indexPath row]] sizeWithFont:[UIFont systemFontOfSize:13.0f] constrainedToSize:CGSizeMake(275, MAXFLOAT) lineBreakMode:UILineBreakModeWordWrap]; cell.title.frame = CGRectMake(50, 6, size1.width, size1.height); CGSize size2 = [[mainEventTimeArray objectAtIndex:[indexPath row]] sizeWithFont:[UIFont systemFontOfSize:11.0f] constrainedToSize:CGSizeMake(275, MAXFLOAT) lineBreakMode:UILineBreakModeWordWrap]; cell.dataTime.frame = CGRectMake(50, 24, size2.width, size2.height); */ cell.title.text = [mainEventArray objectAtIndex:[indexPath row]]; cell.dataTime.text = [mainEventTimeArray objectAtIndex:[indexPath row]]; //if (ente) { // NSLog(@"helllloo"); [cell.title sizeToFit]; [cell.dataTime sizeToFit]; //} return cell; } 

and for heightForRowAtIndexPath method

 - (CGFloat)tableView:(UITableView *)tableView heightForRowAtIndexPath:(NSIndexPath *)indexPath { static NSString *CellIdentifier = @"CustomCell"; CustomCell *cell = (CustomCell *) [tableView dequeueReusableCellWithIdentifier:CellIdentifier]; { if (cell == nil) { NSArray *topLevelObjects = [[NSBundle mainBundle] loadNibNamed:@"CustomCell" owner:nil options:nil]; for (id currentObject in topLevelObjects){ if ([currentObject isKindOfClass:[UITableViewCell class]]){ cell = (CustomCell *) currentObject; break; } } } } CGSize size1 = [[mainEventArray objectAtIndex:[indexPath row]] sizeWithFont:[UIFont systemFontOfSize:13.0f] constrainedToSize:CGSizeMake(230, MAXFLOAT) lineBreakMode:UILineBreakModeWordWrap]; cell.title.frame = CGRectMake(0, 0, size1.width, size1.height); CGSize size2 = [[mainEventTimeArray objectAtIndex:[indexPath row]] sizeWithFont:[UIFont systemFontOfSize:11.0f] constrainedToSize:CGSizeMake(230, MAXFLOAT) lineBreakMode:UILineBreakModeWordWrap]; cell.dataTime.frame = CGRectMake(0, 0, size2.width, size2.height); //NSLog(@"%f", size1.height + size2.height + 20); return size1.height + size2.height + 20; //NSString *str = [heights_ objectAtIndex:[indexPath row]]; // return [str floatValue] ; } 
+4
source share
4 answers

Instead of doing your own customization. Try with dynamic screen height. You will find it really useful and relatively simple than what you have done.

You can check it here: http://www.cimgf.com/2009/09/23/uitableviewcell-dynamic-height/ with full explanation. Enjoy the programming.

+3
source

In your case, you need to set the cell height based on the label.

Check out the link:

http://dcraziee.wordpress.com/2013/05/22/calculate-size-of-uillabel-base-on-text-in/

There is a function called

 -(CGFloat)getHeightForLabel:(NSString *)_str font:(UIFont *)fontOfObject 

Use this function to calculate the height as:

 -(float)tableView:(UITableView *)tableView heightForRowAtIndexPath:(NSIndexPath *)indexPath { CGFloat _height = [self getHeightForLabel:self.label.text font:[self.label font]]; return _height; } 

and do the same when creating and adding labels to the cell. Hope this helps.

0
source
 NSString *reuseIdentifier = @"EventTitleCell"; UITableViewCell *cell = [tableView dequeueReusableCellWithIdentifier:reuseIdentifier]; cell=nil; if (cell == nil) { cell = [[UITableViewCell alloc] initWithStyle:UITableViewCellStyleDefault reuseIdentifier:reuseIdentifier] ; } 
0
source

Try this code.

  label.numberOfLines = 0; // allows label to have as many lines as needed label.text = @"some long text"; [label sizeToFit]; NSLog(@"Label frame is: %@", NSStringFromCGRect(label.frame)); 

For reference: click here

-1
source

Source: https://habr.com/ru/post/1436353/


All Articles