简体   繁体   中英

Dynamic cell height iOS

I'm trying to dynamically recalculate the height of the cell that contain UITextView . The height of the cell depends on the height UITextView.

The source code below recalculate height for each cell in table. This is the some text from NSDictionary . After all heights has recalculated I reload my table view with new height that contain in array heightCells;

UITextView *tempTextView = [[UITextView alloc] init];
UIFont *font = [UIFont fontWithName:@"Helvetica" size:16.0];
[tempTextView setFrame:CGRectMake(10, 63, 300, 9999)];
[tempTextView setFont:font];
NSString *str =  [d valueForKey:@"definition"]; // set long text
tempTextView.text = str;
CGRect frame = tempTextView.frame;
frame.size.height = tempTextView.contentSize.height + 20.0;
tempTextView.frame = frame;
[tempTextView sizeToFit];
int height = tempTextView.frame.size.height + 150.0; // set padding 150.0 px
[heightCells addObject:[NSNumber numberWithFloat:height]];

UITableView method:

- (CGFloat)tableView:(UITableView *)tableView heightForRowAtIndexPath:(NSIndexPath *)indexPath {
    return [[heightCells objectAtIndex:indexPath.row] floatValue];
}

On not retina display everything work good and the height recalculated correct, but on retina display I have problem with recalculation. I know that the retina and not retina have one 320x480 points view and it should not affect for recalculation. But in my case this is appear.

Please see screenshot below. As you can see on screenshot the bottom padding after share button a different for retina and not retina display. And I don't know which kind this problem.

视网膜

不是视网膜

Thanks for help!

It seems the tempTextView 's frame differs when it is on the UITableViewCell . I described my working technique in this answer .

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM