每次旋转更改时,UITableViewCell高度增加2个像素

时间:2011-06-08 14:35:51

标签: ios ios4 uitableview

我有一个带有UITableView插座的UIViewController,我在其中填充了与表视图控制器在同一个nib文件中定义的单元格(设置为ivar outlet)。当我使用旋转功能时,每次旋转到新方向时,第一个单元格都会增长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;
}

2 个答案:

答案 0 :(得分:1)

尝试在第一次需要时将itemNameCell的高度保存到方法中的静态CGFloat,然后再使用它。我猜这只是在你旋转时堆叠起来的UI中的一些自动调整。

这假设您不会想要以编程方式更改单元格的高度(因为您在IB中进行了设置)。

答案 1 :(得分:0)

我也是这样的。当UITableView被分组时,单元格视图的大小调整为2像素。普通样式的UITableView单元格不会出现这种情况。

由于硬编码高度(由Ben建议)不方便(尽管它有效),我的解决方案是实现我自己的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或非nib加载的场景中测试过这个实现。