0

目标:自定义单元格的背景视图。

这是我正在做的

-(UITableViewCell*)tableView:(UITableView*)tableView cellForRowAtIndexPath:(NSIndexPath*)indexPath {
    NSLog(@"cellForRowAtIndexPath cellForRowAtIndexPath");
    static NSString *cellIdentifier = @"CellIdentifier";
    UITableViewCell *cell   =   [tableView dequeueReusableCellWithIdentifier:cellIdentifier];
    if ( cell == nil ) {
        cell = [[UITableViewCell alloc] initWithStyle:UITableViewCellStyleDefault reuseIdentifier:cellIdentifier];
    }
    cell.backgroundView = [[CustomCellBackground alloc] initWithFrame:CGRectMake(0, 0, 200, 80)];

    cell.backgroundColor=   [UIColor purpleColor];

    return cell;
}

CustomCellBackground.m

- (id)initWithFrame:(CGRect)frame
{
    self = [super initWithFrame:frame];
    NSLog(@"frame.x = %f",frame.origin.x);
    NSLog(@"frame.y = %f",frame.origin.y);
    NSLog(@"frame.width = %f",frame.size.width);
    NSLog(@"frame.height = %f",frame.size.height);
    if (self) {
        self.backgroundColor    =   [UIColor purpleColor];

    }
    return self;
}

- (void)drawRect:(CGRect)rect
{
NSLog(@"frame.x = %f",self.frame.origin.x);
NSLog(@"frame.y = %f",self.frame.origin.y);
NSLog(@"frame.width = %f",self.frame.size.width);
NSLog(@"frame.height = %f",self.frame.size.height);


}

在 CustomCellBacground 中,我在 initWithFrame 和 drawRect 方法中打印出框架的大小,而我得到的是

    IN INITWITHFRAME
frame.x = 0.000000
frame.y = 0.000000
frame.width = 200.000000
frame.height = 80.000000

    IN DRAWRECT

//Updated as requested
rect.x = 0.000000
rect.y = 0.000000
rect.width = 302.000000
rect.height = 201.000000

frame.x = 9.000000
frame.y = 0.000000
frame.width = 302.000000
frame.height = 201.000000

问题:为什么drawRect()最近改变了框架的大小。这些值是否应该与initWithFrame

4

1 回答 1

0

单元格调整其大小backgroundView以填充单元格。您传递给的框架initWithFrame:无关紧要。

于 2012-06-15T20:18:22.600 回答