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:
And this is after i start scroll the TableView:
Any Help???
You can set preferred Width property of label. That worked for me
preferred Width
The method
sizeToFit
respects the width of the frame and adjust theheight
. 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 ofsizeToFit
.Solutions:
1) set the width of the frame to your original intended label width everytime before you call
sizeToFit
:2) use your row height calculation and set the frame height instead. No need to use
sizeToFit
.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.
you should probably subclass your cell and clean the UILabel on
If you are not using autolayout, this can also be solved by adding a category on
UIView
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