簡體   English   中英

layoutSubviews在UITableViewCell上調用了兩次

[英]layoutSubviews called twice on UITableViewCell

我在我的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個電話,或者我做錯了什么?

我的猜測是UITableView將單元格的高度調整一個像素,以便為繪制分界線騰出空間。 UITableView為單元格設置新框架后,將再次調用layoutSubviews方法。

暫無
暫無

聲明:本站的技術帖子網頁,遵循CC BY-SA 4.0協議,如果您需要轉載,請注明本站網址或者原文地址。任何問題請咨詢:yoyou2525@163.com.

 
粵ICP備18138465號  © 2020-2024 STACKOOM.COM