目标:自定义单元格的背景视图。
这就是我正在做的事情
-(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 :(得分:0)
单元格调整其backgroundView
的大小以填充单元格。您传递给initWithFrame:
的框架无关紧要。