我有一个带有UITableView出口的UIViewController,我正在用与表视图控制器在同一个nib文件中定义的单元格(设置为ivar出口)填充该出口。当我使用旋转功能时,每当我旋转到新的方向时,第一个单元格就会增长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和表视图代理方法。第一次看到单元高度时,它是界面生成器中定义的默认值,但第二次,它不知何故大了两个像素。这些方法中没有任何代码可以修改任何对象的边界或框架。
有人知道可能发生了什么,或者如何诊断这个问题吗?我想过建立一个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;
}
第一次需要时,试着在方法中将itemNameCell
的高度保存为静态CGFloat
,然后从那时起就使用它。我想这只是UI中的一些自动resizing,随着旋转而堆积起来。
这假设您不希望以编程方式更改单元格的高度(因为您是在IB中设置的)。
我也得到了这个。UITableView分组后,单元格视图的大小似乎调整了2个像素。普通样式的UITableView单元格不会出现这种情况。
由于对高度进行硬编码(正如Ben所建议的)很不方便(尽管它有效),我的解决方案是实现我自己的UITableViewCell子类,并实现rowHeight属性,并在wakeFromNib中设置它。
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;
...
}
注意:我还没有在纯表视图或非nib加载的场景中测试过这个实现。