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
什么在矩形的大小来与你的drawRect程序返回/输出? – trumpetlicks
框架定义视图的位置相对于superviews坐标。由于你的坐标是在“CGPointZero”的右边,我假设一旦视图被添加到单元格中,它就被移动了。另外 - 不要忘记单元格有'contentView'这可能会影响你的背景视图的位置。 – Eimantas
@trumpetlicks:请参阅OP中的更新 – tranvutuan