【发布时间】:2012-01-11 12:48:44
【问题描述】:
我需要在 UITableViewStyleGrouped 表中创建自定义单元格。如果 textLabel 和 detailTextLabel 没有截断显示,它必须看起来像 UITableViewCellStyleValue1,或者像 UITableViewCellStyleSubtitle 一样(但有 detailTextLabel width=contentView.frame.size.width和 UITextAlignmentRight) 如果在 Value1 样式中,其中一个标签被截断。
我需要知道方法- (CGFloat)tableView:(UITableView *)tableView heightForRowAtIndexPath:(NSIndexPath *)indexPath 中cell.contentView 的宽度来决定单元格的高度。我比较 cell.contentView.width 和标签宽度的总和(我使用方法sizeThatFits: 得到它们)来决定它。
那么,如何在单元格创建之前获取正确的 cell.contentView.frame.size.width?
UPD:一些代码来澄清问题。
- (CGFloat)tableView:(UITableView *)tableView heightForRowAtIndexPath:(NSIndexPath *)indexPath
{
CGFloat cellHeight = 44;
CGSize textSize = [[arrayOfTextData objectAtIndex:indexPath.row]
sizeWithFont:[UIFont systemFontOfSize:14]
constrainedToSize:CGSizeMake( 1000, 100)
lineBreakMode:UILineBreakModeCharacterWrap];
CGSize detailedTextSize = [[arrayOfDetailTextData objectAtIndex:indexPath.row]
sizeWithFont:[UIFont systemFontOfSize:14]
constrainedToSize:CGSizeMake(1000, 100)
lineBreakMode:UILineBreakModeCharacterWrap];
if (textSize.width + detailedTextSize.width > /* I need here cell.contentView.frame.size.width*/ 300) {
cellHeight = textSize.height + detailedTextSize.height;
}
return cellHeight;
}
UPD2: 问题是当我创建单元格时 cell.contentView.frame.size.width 等于 cell.frame.size.width 等于 320,但是在单元格之后出现了 cell.contentView。 frame.size.width 变化依赖于 tableView 宽度和 tableView 样式。
- (CGFloat)tableView:(UITableView *)tableView heightForRowAtIndexPath:(NSIndexPath *)indexPath {
static NSString *CellIdentifier = @"tmpCell";
CustomTableViewCellValue1OrSubtitle *cell = [tableView dequeueReusableCellWithIdentifier:CellIdentifier];
if (cell == nil) {
cell = [[[CustomTableViewCellValue1OrSubtitle alloc] initWithStyle:UITableViewCellStyleValue1 reuseIdentifier:CellIdentifier] autorelease];
}
//--Configure the cell
cell.textLabel.text = [arrayOfTextData objectAtIndex:indexPath];
cell.detailTextLabel.text = [arrayOfDetailTextData objectAtIndex:indexPath.row];
[cell layoutIfNeeded];
CGSize textSize = [cell.textLabel.text sizeWithFont:cell.textLabel.font constrainedToSize:CGSizeMake(cell.contentView.bounds.size.width, 99999) lineBreakMode:cell.textLabel.lineBreakMode];
CGSize detailedTextSize = [cell.detailTextLabel.text sizeWithFont:cell.textLabel.font constrainedToSize:CGSizeMake(cell.contentView.bounds.size.width, 99999) lineBreakMode:cell.textLabel.lineBreakMode];
CGFloat cellHeight = cell.contentView.frame.size.height;
NSLog(@"contentView.frame.size.width = %f",cell.contentView.frame.size.width);
//out: contentView.frame.size.width = 320.0
//Always print 320, even if I set Simulator on iPad
if (textSize.width + detailedTextSize.width > cell.contentView.frame.size.width) {
cellHeight = textSize.height + detailedTextSize.height;
}
return cellHeight;
}
【问题讨论】:
-
您找到解决方案了吗?我有一个类似的问题,
contentView.frame.size.width在初始化期间在 iPhone 上返回 320。 -
我最终实现了
layoutSubviews来调整我的自定义单元格内容的大小。 -
我也做过同样的事情。
-
您介意回答问题并将您的答案标记为正确吗?还是
layoutSubviews是您解决特定问题唯一需要做的事情?如果您不想要它,我很乐意得到答案:D
标签: objective-c ios uitableview