Как получить ширину contentView UITableViewCell?

Мне нужно создать пользовательскую ячейку в таблице UITableViewStyleGrouped . Она должна выглядеть как UITableViewCellStyleValue1, если textLabel и detailTextLabel отображаются без усечения, или как UITableViewCellStyleSubtitle (но с detailTextLabtitle. width и UITextAlignmentRight), если в стиле Value1 одна из меток усечена.

Мне нужно знать ширину cell.contentView в методе - (CGFloat) tableView: (UITableView *) tableView heightForRowAtIndexPath: (NSIndexPath *) indexPath , чтобы решить, какой высоты должна быть ячейка. Сравниваю cell.contentView.ширина и сумма ширины этикеток (я получаю их с помощью метода 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. ширина изменяется в зависимости от ширины 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;
}
11
задан Alexander 12 January 2012 в 08:00
поделиться