Высота UITableViewCell увеличивается на 2 пикселя при каждом изменении поворота

#ios #ios4 #uitableview

#iOS #ios4 #uitableview

Вопрос:

У меня есть UIViewController с выходом UITableView, который я заполняю ячейками (настроенными как выходы ivar), которые определены в том же файле nib, что и контроллер представления таблицы. Поскольку я использую функцию поворота, первая ячейка увеличивается на 2 пикселя каждый раз, когда я поворачиваю в новую ориентацию.

Вот вывод с консоли. Я поместил вызов METHOD_LOG в каждый метод класса, чтобы я мог видеть, происходит ли в коде что-то неожиданное, но, похоже, этого не происходит…

 2011-06-08 10:10:42.050 MyApp[10927:207] METHOD_LOG: viewDidLoad /Users/me/path/to/the/source/code/ItemDetailViewController.m
2011-06-08 10:10:42.054 MyApp[10927:207] METHOD_LOG: viewWillAppear: /Users/me/path/to/the/source/code/ItemDetailViewController.m
2011-06-08 10:10:42.054 MyApp[10927:207] METHOD_LOG: shouldAutorotateToInterfaceOrientation: /Users/me/path/to/the/source/code/ItemDetailViewController.m
2011-06-08 10:10:42.056 MyApp[10927:207] METHOD_LOG: numberOfSectionsInTableView: /Users/me/path/to/the/source/code/ItemDetailViewController.m
2011-06-08 10:10:42.056 MyApp[10927:207] METHOD_LOG: tableView:titleForHeaderInSection: /Users/me/path/to/the/source/code/ItemDetailViewController.m
2011-06-08 10:10:42.056 MyApp[10927:207] METHOD_LOG: tableView:titleForHeaderInSection: /Users/me/path/to/the/source/code/ItemDetailViewController.m
2011-06-08 10:10:42.057 MyApp[10927:207] METHOD_LOG: tableView:numberOfRowsInSection: /Users/me/path/to/the/source/code/ItemDetailViewController.m
2011-06-08 10:10:42.057 MyApp[10927:207] METHOD_LOG: tableView:heightForRowAtIndexPath: /Users/me/path/to/the/source/code/ItemDetailViewController.m
2011-06-08 10:10:42.058 MyApp[10927:207] METHOD_LOG: tableView:titleForHeaderInSection: /Users/me/path/to/the/source/code/ItemDetailViewController.m
2011-06-08 10:10:42.058 MyApp[10927:207] METHOD_LOG: tableView:titleForHeaderInSection: /Users/me/path/to/the/source/code/ItemDetailViewController.m
2011-06-08 10:10:42.059 MyApp[10927:207] METHOD_LOG: tableView:numberOfRowsInSection: /Users/me/path/to/the/source/code/ItemDetailViewController.m
2011-06-08 10:10:42.059 MyApp[10927:207] METHOD_LOG: tableView:heightForRowAtIndexPath: /Users/me/path/to/the/source/code/ItemDetailViewController.m
2011-06-08 10:10:42.060 MyApp[10927:207] item name cell height: 44.000000
2011-06-08 10:10:42.060 MyApp[10927:207] METHOD_LOG: tableView:titleForHeaderInSection: /Users/me/path/to/the/source/code/ItemDetailViewController.m
2011-06-08 10:10:42.060 MyApp[10927:207] METHOD_LOG: tableView:titleForHeaderInSection: /Users/me/path/to/the/source/code/ItemDetailViewController.m
2011-06-08 10:10:42.061 MyApp[10927:207] METHOD_LOG: tableView:numberOfRowsInSection: /Users/me/path/to/the/source/code/ItemDetailViewController.m
2011-06-08 10:10:42.061 MyApp[10927:207] METHOD_LOG: tableView:heightForRowAtIndexPath: /Users/me/path/to/the/source/code/ItemDetailViewController.m
2011-06-08 10:10:42.062 MyApp[10927:207] METHOD_LOG: tableView:titleForHeaderInSection: /Users/me/path/to/the/source/code/ItemDetailViewController.m
2011-06-08 10:10:42.062 MyApp[10927:207] METHOD_LOG: tableView:titleForHeaderInSection: /Users/me/path/to/the/source/code/ItemDetailViewController.m
2011-06-08 10:10:42.062 MyApp[10927:207] METHOD_LOG: tableView:numberOfRowsInSection: /Users/me/path/to/the/source/code/ItemDetailViewController.m
2011-06-08 10:10:42.063 MyApp[10927:207] METHOD_LOG: tableView:heightForRowAtIndexPath: /Users/me/path/to/the/source/code/ItemDetailViewController.m
2011-06-08 10:10:42.063 MyApp[10927:207] METHOD_LOG: tableView:titleForHeaderInSection: /Users/me/path/to/the/source/code/ItemDetailViewController.m
2011-06-08 10:10:42.064 MyApp[10927:207] METHOD_LOG: tableView:titleForHeaderInSection: /Users/me/path/to/the/source/code/ItemDetailViewController.m
2011-06-08 10:10:42.064 MyApp[10927:207] METHOD_LOG: tableView:numberOfRowsInSection: /Users/me/path/to/the/source/code/ItemDetailViewController.m
2011-06-08 10:10:42.065 MyApp[10927:207] METHOD_LOG: tableView:heightForRowAtIndexPath: /Users/me/path/to/the/source/code/ItemDetailViewController.m
2011-06-08 10:10:42.065 MyApp[10927:207] METHOD_LOG: tableView:titleForHeaderInSection: /Users/me/path/to/the/source/code/ItemDetailViewController.m
2011-06-08 10:10:42.065 MyApp[10927:207] METHOD_LOG: tableView:titleForHeaderInSection: /Users/me/path/to/the/source/code/ItemDetailViewController.m
2011-06-08 10:10:42.066 MyApp[10927:207] METHOD_LOG: tableView:numberOfRowsInSection: /Users/me/path/to/the/source/code/ItemDetailViewController.m
2011-06-08 10:10:42.066 MyApp[10927:207] METHOD_LOG: tableView:heightForRowAtIndexPath: /Users/me/path/to/the/source/code/ItemDetailViewController.m
2011-06-08 10:10:42.067 MyApp[10927:207] METHOD_LOG: tableView:titleForHeaderInSection: /Users/me/path/to/the/source/code/ItemDetailViewController.m
2011-06-08 10:10:42.067 MyApp[10927:207] METHOD_LOG: tableView:titleForHeaderInSection: /Users/me/path/to/the/source/code/ItemDetailViewController.m
2011-06-08 10:10:42.068 MyApp[10927:207] METHOD_LOG: tableView:numberOfRowsInSection: /Users/me/path/to/the/source/code/ItemDetailViewController.m
2011-06-08 10:10:42.068 MyApp[10927:207] METHOD_LOG: tableView:heightForRowAtIndexPath: /Users/me/path/to/the/source/code/ItemDetailViewController.m
2011-06-08 10:10:42.068 MyApp[10927:207] METHOD_LOG: tableView:titleForHeaderInSection: /Users/me/path/to/the/source/code/ItemDetailViewController.m
2011-06-08 10:10:42.069 MyApp[10927:207] METHOD_LOG: tableView:titleForHeaderInSection: /Users/me/path/to/the/source/code/ItemDetailViewController.m
2011-06-08 10:10:42.069 MyApp[10927:207] METHOD_LOG: tableView:numberOfRowsInSection: /Users/me/path/to/the/source/code/ItemDetailViewController.m
2011-06-08 10:10:42.070 MyApp[10927:207] METHOD_LOG: tableView:heightForRowAtIndexPath: /Users/me/path/to/the/source/code/ItemDetailViewController.m
2011-06-08 10:10:42.070 MyApp[10927:207] METHOD_LOG: tableView:titleForHeaderInSection: /Users/me/path/to/the/source/code/ItemDetailViewController.m
2011-06-08 10:10:42.071 MyApp[10927:207] METHOD_LOG: tableView:titleForHeaderInSection: /Users/me/path/to/the/source/code/ItemDetailViewController.m
2011-06-08 10:10:42.071 MyApp[10927:207] METHOD_LOG: tableView:numberOfRowsInSection: /Users/me/path/to/the/source/code/ItemDetailViewController.m
2011-06-08 10:10:42.071 MyApp[10927:207] METHOD_LOG: tableView:heightForRowAtIndexPath: /Users/me/path/to/the/source/code/ItemDetailViewController.m
2011-06-08 10:10:42.072 MyApp[10927:207] METHOD_LOG: tableView:titleForHeaderInSection: /Users/me/path/to/the/source/code/ItemDetailViewController.m
2011-06-08 10:10:42.072 MyApp[10927:207] METHOD_LOG: tableView:titleForHeaderInSection: /Users/me/path/to/the/source/code/ItemDetailViewController.m
2011-06-08 10:10:42.073 MyApp[10927:207] METHOD_LOG: tableView:numberOfRowsInSection: /Users/me/path/to/the/source/code/ItemDetailViewController.m
2011-06-08 10:10:42.073 MyApp[10927:207] METHOD_LOG: tableView:heightForRowAtIndexPath: /Users/me/path/to/the/source/code/ItemDetailViewController.m
2011-06-08 10:10:42.074 MyApp[10927:207] METHOD_LOG: tableView:titleForHeaderInSection: /Users/me/path/to/the/source/code/ItemDetailViewController.m
2011-06-08 10:10:42.074 MyApp[10927:207] METHOD_LOG: tableView:titleForHeaderInSection: /Users/me/path/to/the/source/code/ItemDetailViewController.m
2011-06-08 10:10:42.074 MyApp[10927:207] METHOD_LOG: tableView:numberOfRowsInSection: /Users/me/path/to/the/source/code/ItemDetailViewController.m
2011-06-08 10:10:42.075 MyApp[10927:207] METHOD_LOG: tableView:heightForRowAtIndexPath: /Users/me/path/to/the/source/code/ItemDetailViewController.m
2011-06-08 10:10:42.075 MyApp[10927:207] METHOD_LOG: tableView:titleForHeaderInSection: /Users/me/path/to/the/source/code/ItemDetailViewController.m
2011-06-08 10:10:42.076 MyApp[10927:207] METHOD_LOG: tableView:titleForHeaderInSection: /Users/me/path/to/the/source/code/ItemDetailViewController.m
2011-06-08 10:10:42.076 MyApp[10927:207] METHOD_LOG: tableView:numberOfRowsInSection: /Users/me/path/to/the/source/code/ItemDetailViewController.m
2011-06-08 10:10:42.077 MyApp[10927:207] METHOD_LOG: tableView:heightForRowAtIndexPath: /Users/me/path/to/the/source/code/ItemDetailViewController.m
2011-06-08 10:10:42.077 MyApp[10927:207] METHOD_LOG: tableView:titleForHeaderInSection: /Users/me/path/to/the/source/code/ItemDetailViewController.m
2011-06-08 10:10:42.077 MyApp[10927:207] METHOD_LOG: tableView:titleForHeaderInSection: /Users/me/path/to/the/source/code/ItemDetailViewController.m
2011-06-08 10:10:42.078 MyApp[10927:207] METHOD_LOG: tableView:numberOfRowsInSection: /Users/me/path/to/the/source/code/ItemDetailViewController.m
2011-06-08 10:10:42.078 MyApp[10927:207] METHOD_LOG: tableView:heightForRowAtIndexPath: /Users/me/path/to/the/source/code/ItemDetailViewController.m
2011-06-08 10:10:42.079 MyApp[10927:207] METHOD_LOG: tableView:titleForHeaderInSection: /Users/me/path/to/the/source/code/ItemDetailViewController.m
2011-06-08 10:10:42.079 MyApp[10927:207] METHOD_LOG: tableView:titleForHeaderInSection: /Users/me/path/to/the/source/code/ItemDetailViewController.m
2011-06-08 10:10:42.080 MyApp[10927:207] METHOD_LOG: tableView:numberOfRowsInSection: /Users/me/path/to/the/source/code/ItemDetailViewController.m
2011-06-08 10:10:42.080 MyApp[10927:207] METHOD_LOG: tableView:heightForRowAtIndexPath: /Users/me/path/to/the/source/code/ItemDetailViewController.m
2011-06-08 10:10:42.080 MyApp[10927:207] METHOD_LOG: tableView:titleForHeaderInSection: /Users/me/path/to/the/source/code/ItemDetailViewController.m
2011-06-08 10:10:42.081 MyApp[10927:207] METHOD_LOG: tableView:titleForHeaderInSection: /Users/me/path/to/the/source/code/ItemDetailViewController.m
2011-06-08 10:10:42.081 MyApp[10927:207] METHOD_LOG: tableView:numberOfRowsInSection: /Users/me/path/to/the/source/code/ItemDetailViewController.m
2011-06-08 10:10:42.082 MyApp[10927:207] METHOD_LOG: tableView:heightForRowAtIndexPath: /Users/me/path/to/the/source/code/ItemDetailViewController.m
2011-06-08 10:10:42.082 MyApp[10927:207] METHOD_LOG: tableView:titleForHeaderInSection: /Users/me/path/to/the/source/code/ItemDetailViewController.m
2011-06-08 10:10:42.082 MyApp[10927:207] METHOD_LOG: tableView:titleForHeaderInSection: /Users/me/path/to/the/source/code/ItemDetailViewController.m
2011-06-08 10:10:42.083 MyApp[10927:207] METHOD_LOG: tableView:numberOfRowsInSection: /Users/me/path/to/the/source/code/ItemDetailViewController.m
2011-06-08 10:10:42.083 MyApp[10927:207] METHOD_LOG: tableView:heightForRowAtIndexPath: /Users/me/path/to/the/source/code/ItemDetailViewController.m
2011-06-08 10:10:42.084 MyApp[10927:207] METHOD_LOG: tableView:cellForRowAtIndexPath: /Users/me/path/to/the/source/code/ItemDetailViewController.m
2011-06-08 10:10:42.085 MyApp[10927:207] METHOD_LOG: tableView:cellForRowAtIndexPath: /Users/me/path/to/the/source/code/ItemDetailViewController.m
2011-06-08 10:10:42.086 MyApp[10927:207] METHOD_LOG: tableView:cellForRowAtIndexPath: /Users/me/path/to/the/source/code/ItemDetailViewController.m
2011-06-08 10:10:42.087 MyApp[10927:207] METHOD_LOG: tableView:titleForHeaderInSection: /Users/me/path/to/the/source/code/ItemDetailViewController.m
2011-06-08 10:10:42.088 MyApp[10927:207] METHOD_LOG: tableView:titleForHeaderInSection: /Users/me/path/to/the/source/code/ItemDetailViewController.m
2011-06-08 10:10:42.088 MyApp[10927:207] METHOD_LOG: tableView:titleForHeaderInSection: /Users/me/path/to/the/source/code/ItemDetailViewController.m
2011-06-08 10:10:49.619 MyApp[10927:207] METHOD_LOG: shouldAutorotateToInterfaceOrientation: /Users/me/path/to/the/source/code/ItemDetailViewController.m
2011-06-08 10:10:49.622 MyApp[10927:207] METHOD_LOG: numberOfSectionsInTableView: /Users/me/path/to/the/source/code/ItemDetailViewController.m
2011-06-08 10:10:49.623 MyApp[10927:207] METHOD_LOG: tableView:titleForHeaderInSection: /Users/me/path/to/the/source/code/ItemDetailViewController.m
2011-06-08 10:10:49.623 MyApp[10927:207] METHOD_LOG: tableView:titleForHeaderInSection: /Users/me/path/to/the/source/code/ItemDetailViewController.m
2011-06-08 10:10:49.624 MyApp[10927:207] METHOD_LOG: tableView:numberOfRowsInSection: /Users/me/path/to/the/source/code/ItemDetailViewController.m
2011-06-08 10:10:49.624 MyApp[10927:207] METHOD_LOG: tableView:heightForRowAtIndexPath: /Users/me/path/to/the/source/code/ItemDetailViewController.m
2011-06-08 10:10:49.625 MyApp[10927:207] METHOD_LOG: tableView:titleForHeaderInSection: /Users/me/path/to/the/source/code/ItemDetailViewController.m
2011-06-08 10:10:49.625 MyApp[10927:207] METHOD_LOG: tableView:titleForHeaderInSection: /Users/me/path/to/the/source/code/ItemDetailViewController.m
2011-06-08 10:10:49.625 MyApp[10927:207] METHOD_LOG: tableView:numberOfRowsInSection: /Users/me/path/to/the/source/code/ItemDetailViewController.m
2011-06-08 10:10:49.626 MyApp[10927:207] METHOD_LOG: tableView:heightForRowAtIndexPath: /Users/me/path/to/the/source/code/ItemDetailViewController.m
2011-06-08 10:10:49.626 MyApp[10927:207] item name cell height: 46.000000
  

Как вы можете видеть, после загрузки представления вызываются только методы shouldAutorotateToInterfaceOrientation и методы делегирования табличного представления. В первый раз, когда вы видите высоту ячейки, это значение по умолчанию, как определено в Interface Builder, но во второй раз оно почему-то на два пикселя больше. Ни в одном из этих методов нет кода, который бы изменял границы или фрейм какого-либо объекта вообще.

У кого-нибудь есть идеи о том, что может происходить или как диагностировать проблему? Я думал о настройке KVO, но я не думаю, что смогу добраться до рамки ячейки с этим.

РЕДАКТИРОВАТЬ: Вот код, который возвращает высоту строки по определенному пути индекса:

 - (CGFloat)tableView:(UITableView *)tableView heightForRowAtIndexPath:(NSIndexPath *)indexPath
{
    METHOD_LOG;

    int idx = [indexPath section];

    if (idx == SECTION_ITEM)
    {
        NSLog(@"item name cell height: %f", itemNameCell.frame.size.height);
        return itemNameCell.frame.size.height;
    }
    else if (idx == SECTION_DESCRIPTION)
    {
        return itemDescriptionCell.frame.size.height;
    }
    ...
    ... other section identifiers here, you get the idea
    ...

    return 44.0;
}
  

ПРАВКА 2: Вот код, который создает и возвращает ячейки:

 - (UITableViewCell *)tableView:(UITableView *)tv cellForRowAtIndexPath:(NSIndexPath *)indexPath
{
    METHOD_LOG;

    UITableViewCell *cell;

    int idx = [indexPath section];

    if (idx == SECTION_ITEM)
    {
        cell = itemNameCell;
        UILabel *label = (UILabel *)[cell viewWithTag:1];
        label.text = [[jobItem jItemRef] blankIfNULL];
    }
    else if (idx == SECTION_DESCRIPTION)
    {
        cell = itemDescriptionCell;
        UITextView *textView = (UITextView *)[cell viewWithTag:1];
        textView.text = [[jobItem jItemDesc] blankIfNULL];
    }
    ...
    ... again, you get the idea
    ...

    return cell;
}
  

Комментарии:

1. Как это cellForRowAtIndexPath: реализовано?

Ответ №1:

Попробуйте сохранить itemNameCell высоту в статическом CGFloat значении в вашем методе в первый раз, когда это необходимо, а затем просто используйте это с тех пор. Я бы предположил, что это просто некоторый авторазмер в пользовательском интерфейсе, который увеличивается при повороте.

Это предполагает, что вы не захотите изменять высоту ячейки программно (поскольку вы настраиваете ее в IB).

Комментарии:

1. Да, я знаю, что мог бы это сделать, я надеялся добраться до корня проблемы. Я вижу эту же проблему на другом контроллере просмотра в том же приложении, которое очень похоже.

2. Я просто жестко запрограммировал значения высоты на данный момент, поскольку мне нужно перейти к другим проблемам. Если у кого-нибудь есть какие-либо идеи в ближайшем будущем, пожалуйста, опубликуйте, и я посмотрю. Спасибо.

Ответ №2:

Я также получаю это. Кажется, что при группировании UITableView размер ячейки изменяется на 2 пикселя. Это не происходит с ячейками UITableView в обычном стиле.

Поскольку жесткое кодирование высоты (как предложил Бен) было неудобным (хотя это работает), моим решением было реализовать мой собственный подкласс UITableViewCell и реализовать свойство RowHeight и установить это в awakeFromNib.

MyTableViewCell.h

 @interface MyTableViewCell : UITableViewCell {
    CGFloat rowHeight;
}

@property (nonatomic) CGFloat rowHeight;

@end
  

MyTableViewCell.m

 @implementation MyTableViewCell

@synthesize rowHeight;

-(void)awakeFromNib {
    self.rowHeight = self.frame.size.height;
}

@end
  

Затем в вашем делегате TableView,

 -(CGFloat)tableView:(UITableView *)theTableView heightForRowAtIndexPath:(NSIndexPath *)indexPath {
...
return itemDescriptionCell.rowHeight;
...
}
  

Внимание: Я не тестировал эту реализацию в обычном tableview или сценариях без загрузки кончика.