Reputation: 4748
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
Upvotes: 0
Views: 1449
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
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