Мне нужно создать пользовательскую ячейку в UITableViewStyleGrouped таблице. Должно выглядеть как UITableViewCellStyleValue1, если textLabel и detailTextLabel отображаются без усечения или как UITableViewCellStyleSubtitle (но с detailTextLabel.sizeView.set width.ize =. UITextAlignmentRight), если в стиле Value1 одна из меток усекается.
Мне нужно знать ширину cell.contentView в методе - (CGFloat)tableView:(UITableView *)tableView heightForRowAtIndexPath:(NSIndexPath *)indexPath
, чтобы решить, какой высоты должна быть ячейка. Я сравниваю 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;
}