我有一个UITableView (分组!)并需要计算两种类型的单元格的高度:UITableViewCellStyleDefault和UITableViewCellStyleValue2。
这是我为UITableViewCellStyleDefault**:** 所做的
CGSize textSize = {300.f, 200000.0f};
CGSize size = [myTextString1 sizeWithFont:[UIFont systemFontOfSize:14.0f] constrainedToSize:textSize lineBreakMode:UILineBreakModeWordWrap];
size.height += 30.0f;
result = MAX(size.height, 44.0f);UITableViewCellStyleValue2**:** 和
CGSize textSize = {207.f, 200000.0f};
CGSize size = [myTextString2 sizeWithFont:[UIFont systemFontOfSize:14.0f] constrainedToSize:textSize lineBreakMode:UILineBreakModeWordWrap];
size.height += 30.0f;
result = MAX(size.height, 44.0f);我的问题是,他们返回不正确的高度,我认为这是textSize,我使用不正确的数字。对于长文本,底部的部分会被剪短(通常只有几个单词的一行),对于两个CellStyles,它们在单元格中的文本上下都有奇怪的间距。
对于UITableViewCellStyleValue2,我通过将text.backgroundColor变成红色,然后计算盒子的大小,得到了宽度大小(207.0f)。300.0f UITableViewCellStyleDefault的宽度是UITableViewStyleGrouped的单元格有多大。
有人知道我需要使用哪些值来正确计算NSString的大小,从而为我的单元格获得适当的高度吗?
谢谢
发布于 2009-07-24 22:18:56
下面是我为此使用的代码。对于一种细胞来说,它就像一种魅力。对于您的应用程序,它可能有一些有用的部分。
- (CGFloat) tableView:(UITableView *)tableView heightForRowAtIndexPath: (NSIndexPath *) indexPath {
AppAppDelegate *appDelegate = (AppAppDelegate *)[[UIApplication sharedApplication] delegate];
NSString *Text = ([[appDelegate.myTextSectionsDelegateDict objectAtIndex:indexPath.section] objectForKey:@"Text"]);
UIFont *cellFont = [UIFont fontWithName:@"Helvetica" size:17.0];
CGSize constraintSize = CGSizeMake(280.0f, MAXFLOAT);
CGSize labelSize = [Text sizeWithFont:cellFont constrainedToSize:constraintSize lineBreakMode:UILineBreakModeWordWrap];
return labelSize.height + 15;}发布于 2009-07-21 18:49:07
创建单元格时,是否使用与用于测量的字体相同的字体?
我使用了此页上的教程,一切都对我有用。它也可能对你有用:
发布于 2009-07-30 04:15:26
它看到你发现你使用了错误的文本大小。您可能只想使用标签的font和lineBreakMode属性来避免将来出现这个问题,特别是以后在单元格中更改它们时。此外,为了可读性起见,在返回数字之前,我将避免增加高度。相反,我会尝试这样的方法:
CGSize textSize = CGSizeMake( 300.0, 1979 );
CGSize size = [ myTextString1 sizeWithFont:[[ cell textLabel ] font ]
constrainedToSize:textSize
lineBreakMode:[[ cell textLabel ] lineBreakMode ]];
result = MAX( size.height + 30, 44.0f );我使用1979年是因为根据文档,不应该返回大于2009年的值。
https://stackoverflow.com/questions/1160765
复制相似问题