Evening,
I have several UITableViewCell’s which expand dynamically in height based on the text contained within them, but it appears that once a certain number of characters are entered (although I’ve not yet tested this so I’m not sure it’s the exact cause), the height is then calculated incorrectly.
Here are the relevant parts of my code, with an example string that causes an issue, purposely truncated to 1000 characters (the max length for the field the user can type into):
Objective-C is a reflective, object-oriented programming language that adds Smalltalk-style messaging to the C programming language.
Today, it is used primarily on Apple's Mac OS X and iOS: two environments derived from the OpenStep standard, though not compliant with it.[1] Objective-C is the primary language used for Apple's Cocoa API, and it was originally the main language on NeXT's NeXTSTEP operating system. Generic Objective-C programs that do not use these libraries can also be compiled for any system supported by gcc or Clang.
Objective-C is a reflective, object-oriented programming language that adds Smalltalk-style messaging to the C programming language.
Today, it is used primarily on Apple's Mac OS X and iOS: two environments derived from the OpenStep standard, though not compliant with it.[1] Objective-C is the primary language used for Apple's Cocoa API, and it was originally the main language on NeXT's NeXTSTEP operating system. Generic Objective-C programs that do no
This is from my cellForRowAtIndexPath:..
cell.textLabel.font = [UIFont boldSystemFontOfSize:15];
cell.detailTextLabel.font = [UIFont systemFontOfSize:15];
cell.detailTextLabel.lineBreakMode = UILineBreakModeWordWrap;
cell.detailTextLabel.numberOfLines = 0;
I then calculate the cell height using the following:
- (CGFloat)tableView:(UITableView *)t heightForRowAtIndexPath:(NSIndexPath *)indexPath {
NSString *cellLabel;
NSString *cellText;
if (indexPath.section == 0) {
// This returns a small string such as 'Note'
cellLabel = [generalDetailsCellsArray objectAtIndex:indexPath.row];
// This returns the example string posted in the above block
cellText = [generalDetailsValuesArray objectAtIndex:indexPath.row];
}
--- snip ---
CGSize constraint = CGSizeMake(300.0f, 20000.0f);
CGSize labelSize = [cellLabel sizeWithFont:[UIFont boldSystemFontOfSize:15] constrainedToSize:constraint];
CGSize textSize = [cellText sizeWithFont:[UIFont systemFontOfSize:15] constrainedToSize:constraint];
return (labelSize.height + textSize.height) + 12;
}
Can anyone see any glaringly obvious mess-ups?
Thanks in advance.
Should anyone else come across this question, don’t make the same mistake as I did!
CGSize constraint = CGSizeMake(300.0f, 20000.0f)should beCGSize constraint = CGSizeMake(280.0f, 20000.0f), I wasn’t taking in to account the cell padding inside the cell so I was calculating a label width wider than what it was when drawn in to the cell.In short: A label inside a grouped UITableViewCell has a 280 width!