我需要自定义单元格中UITableViewStyleGrouped table.It必须如下UITableViewCellStyleValue1如果为textLabel和detailTextLabel都没有截断显示,或作为UITableViewCellStyleSubtitle(但detailTextLabel宽度= contentView.frame.size.width和UITextAlignmentRight)如果值1样式之一的标签被截断。如何获取UITableViewCell的contentView宽度?
我需要知道方法- (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;
}
您是否曾经为此找到过解决方案?我有一个类似的问题,在初始化期间,iPhone上的contentView.frame.size.width将返回320。 – cod3monk3y 2013-01-09 17:47:29
我最终实现了'layoutSubviews'来调整我的自定义单元格内容。 – cod3monk3y 2013-01-09 18:01:09
我做了同样的事情。 – Alexander 2013-01-10 05:08:31