Abdalrahman Shatou
Abdalrahman Shatou

Reputation: 4748

iOS - Reload UITableView to Fix Cell Label Height

Well, this sounds totally weird and I have been spending the last 3 hours trying to fix it. I have a table view with custom cell that contains two labels. The labels heights are variables and calculated using the method sizeWithFont:constrainedToSize:lineBreakMode.

cell = [tableView dequeueReusableCellWithIdentifier:sCellIdentifier];

cell.selectionStyle = UITableViewCellSelectionStyleNone;

cell.tariffPlanCellContentView.cellPrefixLabel.text = [NSString stringWithFormat:sPrefixStringWithColon, [self getPrefixStringAtIndexPath:indexPath]];
cell.tariffPlanCellContentView.cellPrefixLabel.textAlignment = UITextAlignmentLeft;
cell.tariffPlanCellContentView.cellPrefixLabel.frame = CGRectMake(cell.tariffPlanCellContentView.cellPrefixLabel.frame.origin.x,
                                                                  cell.tariffPlanCellContentView.cellPrefixLabel.frame.origin.y,
                                                                  kTableViewCellLabelWidth,
                                                                  [self getSizeForPrefixStringAtIndexPath:indexPath].height);

cell.tariffPlanCellContentView.cellDetailsLabel.text = [self getDetailStringAtIndexPath:indexPath];
cell.tariffPlanCellContentView.cellDetailsLabel.frame = CGRectMake(cell.tariffPlanCellContentView.cellDetailsLabel.frame.origin.x,
                                                                   cell.tariffPlanCellContentView.cellPrefixLabel.frame.origin.y + cell.tariffPlanCellContentView.cellPrefixLabel.frame.size.height + 3,
                                                                   kTableViewCellLabelWidth,
                                                                   [self getSizeForDetailsStringAtIndexPath:indexPath].height);

[cell.tariffPlanCellContentView.cellPrefixLabel sizeToFit];
[cell.tariffPlanCellContentView.cellDetailsLabel sizeToFit];

At the first load of the table view, all cells appear with one line only. Only when I scroll the table view (or programmatically reloading the table view "again"), the labels are fixed! I don't know why it's doing that? cellPrefixLabel and cellDetailsLabel are inside a custom cell loaded from nib. Both have numberOfLines = 0

Update

  1. I am not using Auto-Layout, I am still supporting iOS 5.
  2. The application is working fine on iOS 6 and the bug is only on iOS 7 beta 6! Is it due to the beta version? Does any one face anything like this?
  3. I removed sizeToFit and the same problem exists. In fact, sizeToFit doesn't help at all and it's not required on iOS 6.

Upvotes: 0

Views: 1449

Answers (3)

Abdalrahman Shatou
Abdalrahman Shatou

Reputation: 4748

Ok, I finally managed to know why. It is in fact a bug (or this is how it looks). The method sizeWithFont... is used with one of iOS 7 new font text styles. It seems that this method doesn't count for this new font correctly. Hopefully, tomorrow's GM would fix this bug. If not, I have to use a workaround by adding about 10 points to the height.

What does this has to do with a reload fixing the height (if using sizeToFit), I really don't know. The whole new dynamic font system seems fragile.

Upvotes: 0

slysid
slysid

Reputation: 5498

Move all you UITableviewcell additions code to the delegate method and give it a try

- (void)tableView:(UITableView *)tableView willDisplayCell:(UITableViewCell *)cell forRowAtIndexPath:(NSIndexPath *)indexPath

Upvotes: 1

TomSwift
TomSwift

Reputation: 39512

Are you using auto-layout? If so then you probably shouldn't be calling sizeToFit. The size of the label would be inferred from its intrinsicContentSize, which is updated after the text is set.

See my answer here for additional insights.

Upvotes: 0

Related Questions