ios - 动态单元格高度 iOS

标签 ios uitableview

我正在尝试动态重新计算包含 UITextView 的单元格的高度。单元格的高度取决于 UITextView 的高度。

下面的源代码重新计算表格中每个单元格的高度。这是来自 NSDictionary 的一些文本。重新计算所有高度后,我使用数组 heightCells 中包含的新高度重新加载表格 View ;

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方法:

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

在非视网膜显示屏上,一切正常,并且重新计算的高度正确,但在视网膜显示屏上,我在重新计算时遇到问题。我知道视网膜而不是视网膜有一个 320x480 点 View ,它不应该影响重新计算。但就我而言,这是出现的。

请参阅下面的屏幕截图。正如您在屏幕截图中看到的,共享按钮后的底部填充对于视网膜和非视网膜显示是不同的。而且我不知道这个问题属于哪一类。

retina

not retina

感谢您的帮助!

最佳答案

tempTextView 位于 UITableViewCell 上时,它的框架似乎有所不同。我在这个answer中描述了我的工作技巧.

关于ios - 动态单元格高度 iOS,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/10566736/

相关文章:

ios - UITableViews 之间的间距,如设置应用程序

ios - 使用不同类型覆盖属性

ios - UITableView 和 UITableViewDataSource

ios - 设置 UITableHeaderFooterView 的 backgroundColor 时出错

objective-c - If-Else 语句被翻转

ios - iOS 中每个国家/地区的 IAP 定价不同

ios - 如何在iOS UIButton中添加投影?

ios - UILabel 滚动后不遵守约束(xib 内的标签,作为 uitableviewcell 排队)

ios - 如果已经建立,如何将用户添加到 QBRTCSession

android - 自动在 Play Store 或 App Store 中搜索应用程序