UITableView with UILabel SizeToFit get mess when S

2020-07-10 05:24发布

问题:

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???

回答1:

you should probably subclass your cell and clean the UILabel on

 override func prepareForReuse() {
    super.prepareForReuse()
    self.label.text = nil
}


回答2:

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.



回答3:

  • This might help http://www.cimgf.com/2009/09/23/uitableviewcell-dynamic-height/

  • The approach in given reference and solution provided by @Verbumdei is nearby same.



回答4:

If you are not using autolayout, this can also be solved by adding a category on UIView

public func sizeToFit(constrainedSize: CGSize) -> CGSize {
  var newSize = sizeThatFits(constrainedSize)
  newSize.width = min(newSize.width, constrainedSize.width)
  newSize.height = min(newSize.height, constrainedSize.height)
  self.size = newSize
  return newSize
}

The idea is to use sizeThatFits with a constrained size to determine the size to adopt and then use the min expected width/height.

Then you just have to do something like

yourLabel.sizeToFit(self.contentView.size) // or
yourLabel.sizeToFit(CGSize(width: maxWidth, height: self.contentView.size.height))


回答5:

You can set preferred Width property of label. That worked for me

preferred Width