2012-06-15 68 views
0

GOAL不同:爲了定製單元格的背景圖。寬度和框架的高度是在initWithFrame和drawRect中

下面是我在做什麼

-(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

+0

什麼在矩形的大小來與你的drawRect程序返回/輸出? – trumpetlicks

+1

框架定義視圖的位置相對於superviews座標。由於你的座標是在「CGPointZero」的右邊,我假設一旦視圖被添加到單元格中,它就被移動了。另外 - 不要忘記單元格有'contentView'這可能會影響你的背景視圖的位置。 – Eimantas

+0

@trumpetlicks:請參閱OP中的更新 – tranvutuan

回答

0

相同,則單元調整其backgroundView以填充單元格。你傳遞給initWithFrame:的框架是無關緊要的。