简体   繁体   English

动态单元格高度iOS

[英]Dynamic cell height iOS

I'm trying to dynamically recalculate the height of the cell that contain UITextView . 我试图动态地重新计算包含UITextView的单元格的高度。 The height of the cell depends on the height UITextView. 单元格的高度取决于UITextView的高度。

The source code below recalculate height for each cell in table. 下面的源代码重新计算了表格中每个单元格的高度。 This is the some text from NSDictionary . 这是NSDictionary的一些文字。 After all heights has recalculated I reload my table view with new height that contain in array heightCells; 重新计算所有高度后,我用包含在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: UITableView方法:

- (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. 我知道视网膜(而不是视网膜)具有一个320x480点的视图,并且不应影响重新计算。 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 . 看起来tempTextView的框架在UITableViewCell上时有所不同。 I described my working technique in this answer . 我在这个答案中描述了我的工作技巧。

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

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