Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

UITableView with UILabel SizeToFit get mess when Scrolling

Hi everyone I have problem with my tableview, i make Cell with uilabel with SizeToFit and then calculate the UILabel Height for set the cell Height everything work well except when i scrolling my tableView the text get weird like one char per line:

My TableViewVell Method is:

-(UITableViewCell *)tableView:(UITableView *)tableView cellForRowAtIndexPath:(NSIndexPath *)indexPath
{
static NSString *CellIdentifier = @"Cell";
UITableViewCell *cell = [tableView dequeueReusableCellWithIdentifier:CellIdentifier forIndexPath:indexPath];

if (cell == nil) {
    cell = [[UITableViewCell alloc] initWithStyle:UITableViewCellStyleDefault reuseIdentifier:CellIdentifier];
    cell.accessoryType = UITableViewCellAccessoryDisclosureIndicator;
}


UILabel *label = (UILabel *)[cell viewWithTag:1];
label.text = [someArray objectAtIndex:indexPath.row];
// Set the UILabel to fit my text;
messageLabel.lineBreakMode = UILineBreakModeWordWrap;
messageLabel.numberOfLines = 0;
[messageLabel sizeToFit];


return cell;   
}

The Cell Height stay in the correct Size the problem only with the UILabel ... this the result when i load the view:

First screenshot

And this is after i start scroll the TableView:

Second screenshot

Any Help???

like image 460
Dekel Maman Avatar asked Jun 05 '13 20:06

Dekel Maman


2 Answers

The method sizeToFit respects the width of the frame and adjust the height. And table view cell reuse the cells. Combining those two behaviors can cause the issue you described.

One of your label, which is very short in content (e.g. in the 4th cell with the word "jio"), is resized with sizeToFit and the resulting width is smaller than the original width you intended.

After that, table view reuse the cell and sizeToFit still respects the small width calculated from previous call of sizeToFit.

Solutions:

1) set the width of the frame to your original intended label width everytime before you call sizeToFit:

CGRect frame = messageLabel.frame;
frame.size.width = 100.0; //you need to adjust this value 
messageLabel.frame = frame;

2) use your row height calculation and set the frame height instead. No need to use sizeToFit.

like image 177
Valent Richie Avatar answered Oct 05 '22 22:10

Valent Richie


you should probably subclass your cell and clean the UILabel on

 override func prepareForReuse() {
    super.prepareForReuse()
    self.label.text = nil
}
like image 20
Rotem Doron Avatar answered Oct 05 '22 22:10

Rotem Doron