layoutSubviews在UITableViewCell上调用了两次

时间:2012-07-18 17:33:46

标签: ios

我在我的UITableViewCell子类中覆盖layoutSubviews。我注意到每个单元格都会调用layoutSubviews两次。在第二次调用时,内容视图框架高度比第一次调用时的高度小1:

@implementation MyUITableViewCellCell

+ (NSString *)asString:(CGRect) rect {
     NSString *res = [[NSString alloc] initWithFormat:@"[%f %f %f %f]",
                 rect.origin.x, rect.origin.y, rect.size.width, rect.size.height];
     [res autorelease];
     return res;
}

- (void)layoutSubviews
{
    [super layoutSubviews];
    NSLog(@"Here I am %@ frame=%@ cvframe=%@,
      self.text, 
      [MyUITableViewCellCell asString:self.frame],
      [MyUITableViewCellCell asString:self.contentView.frame]);
}

@end

以下是控制器如何创建表格单元格:

- (NSString*)dataAtIndex:(NSInteger)index
{
    NSString* data = [[NSString alloc] initWithFormat:@"Row %d", index];
    return [data autorelease];
}

- (CGFloat)tableView:(UITableView *)tableView heightForRowAtIndexPath:(NSIndexPath     *)indexPath
{
    return 30;
}

- (UITableViewCell *)tableView:(UITableView *)tableView cellForRowAtIndexPath:(NSIndexPath *)indexPath
{
    static NSString *CellIdentifier = @"Alex";

    NSInteger index = [indexPath row];
    MyUITableViewCellCell* cell = [tableView dequeueReusableCellWithIdentifier:CellIdentifier];
    if (cell == nil) {
        cell = [[[MyUITableViewCellCell alloc] initWithStyle:UITableViewCellStyleDefault reuseIdentifier:CellIdentifier] autorelease];
    }
    cell.text = [self dataAtIndex:index];
    return cell;
}

输出:

Here I am Row 0 frame=[0.000000 0.000000 320.000000 30.000000] cvframe=[0.000000 0.000000 320.000000 30.000000]
Here I am Row 1 frame=[0.000000 30.000000 320.000000 30.000000] cvframe=[0.000000 0.000000 320.000000 30.000000]
Here I am Row 0 frame=[0.000000 0.000000 320.000000 30.000000] cvframe=[0.000000 0.000000 320.000000 29.000000]
Here I am Row 1 frame=[0.000000 30.000000 320.000000 30.000000] cvframe=[0.000000 0.000000 320.000000 29.000000]

预计每个小区有2个电话,或者我做错了什么?

1 个答案:

答案 0 :(得分:1)

我的猜测是UITableView将单元格的高度调整一个像素,以便为绘制分割线腾出空间。在layoutSubviews为单元格设置新框架后,将再次调用UITableView方法。

相关问题