UITableView cell content going past the cell graphic
I'm using heightForRowAtIndexPath and sizeWithFont to calculate the height for a cell. After 3 or 4 lines of text, the label containing the text grows past the top and bottom of the cell's view. Anything from 0-3 lines grows and shrinks properly.
I can't find where the 开发者_如何学Pythoncell would have a hard cap on height.
- (CGFloat)tableView:(UITableView *)tableView heightForRowAtIndexPath:(NSIndexPath *)indexPath;
{
NSArray *array = [sectionsArray objectAtIndex:indexPath.section];
NSDictionary *dictionary = [array objectAtIndex:indexPath.row];
id key = [[dictionary allKeys] objectAtIndex:0];
NSString *keyString = [NSString stringWithFormat:@"%@", key];
NSString *keyContents = [NSString stringWithFormat:@"%@", [dictionary objectForKey:key]];
CGFloat height = 44;
if ([keyString isEqualToString:@"Notes"]) {
if ([keyContents length] > 0) {
CGSize constraint = CGSizeMake(320.0f - (10.0f * 2), 20000.0f);
height = 20 + [keyContents sizeWithFont:[UIFont boldSystemFontOfSize:15] constrainedToSize:constraint lineBreakMode:UILineBreakModeWordWrap].height;
}
}
NSLog(@"Height of %@ with content %@ is %f", keyString, keyContents, height);
if (height <= 44) {
return 44;
}
else {
return height;
}
}
Here's a screenshot of what a too large entry looks like.
Your constraint width is too large. You are assuming 320 minus the 10 padding to either side, when in fact the text label is smaller than that due to your choice of table view cell style. At a guess, try a constraint width of 200 or 180 (or a value derived from experimentation or printing out the label's bounds.)
精彩评论