我正在制作一个表格视图,用户可以在其中选择应用程序应在其中导入的日历,我的目的是制作类似于 Cal 的内容:https ://www.dropbox.com/s/i980yr70j0vq0p1/foto.PNG
所以我创建了 TableViewController 并在 tableViewCellForRowAtIndexPath 中创建了我的自定义单元格:
- (UITableViewCell *)tableView:(UITableView *)tableView cellForRowAtIndexPath:(NSIndexPath *)indexPath{
static NSString *MyIdentifier = @"MyReuseIdentifier";
EKCalendar *calendar;
calendar = [theCalendarSyncEngine.calendars objectAtIndex:indexPath.row];
CalendarCell *cell = [tableView dequeueReusableCellWithIdentifier:MyIdentifier];
if(cell==nil){
cell = [[CalendarCell alloc]initAndReuseIdentifier:MyIdentifier andIsChecked:FALSE andAColor:calendar.CGColor];
}
cell.title.text=calendar.title;
return cell;
}
CalendarCell 初始化方法:
- (id)initAndReuseIdentifier:(NSString *)reuseIdentifier andIsChecked:(BOOL)checked andAColor:(CGColorRef)color
{
self = [super initWithStyle:UITableViewCellStyleSubtitle reuseIdentifier:reuseIdentifier];
if (self) {
// Initialization code
theColor=color;
positionFrame = CGRectMake(30,self.contentView.bounds.size.height/2-2.5,5,5);
circle=[[CircleView alloc]initWithFrame:positionFrame andAColor:theColor];
title = [[UILabel alloc] initWithFrame:CGRectMake(50.0, self.contentView.bounds.size.height/2-10, 230.0, 20.0)];
title.backgroundColor = [UIColor clearColor];
title.textAlignment = NSTextAlignmentLeft;
title.textColor = [UIColor blackColor];
title.font = [UIFont boldSystemFontOfSize:17.0];
checkbox = [[UISelectionCheckbox alloc] initWithFrame:CGRectMake(270.0, self.contentView.bounds.size.height/2-10, 20, 20)];
checkbox.backgroundColor = [UIColor clearColor];
checkbox.checked = checked;
[checkbox addTarget:self action:@selector(checkBoxTouched:) forControlEvents:UIControlEventTouchUpInside];
[self.contentView addSubview:circle];
[self.contentView addSubview:circle];
[self.contentView addSubview:title];
[self.contentView addSubview:checkbox];
}
return self;
}
日历单元格有一个 CircleView,它是一个带有日历颜色的圆圈、一个带有日历名称的标签和一个复选框,用于选择或不选择日历。
这是 CircleView 代码:
- (id)initWithFrame:(CGRect)frame andAColor:(CGColorRef)color
{
self = [super initWithFrame:frame];
if (self) {
[self setBackgroundColor:[UIColor clearColor]];
thisColor = color;
}
return self;
}
- (void)drawRect:(CGRect)rect{
CGContextRef context= UIGraphicsGetCurrentContext();
CGContextClearRect(context,rect);
CGContextSetFillColorWithColor(context, thisColor);
CGContextSetAlpha(context, 1);
CGContextFillEllipseInRect(context, CGRectMake(0,0,self.frame.size.width,self.frame.size.height));
CGContextSetStrokeColorWithColor(context, thisColor);
}
问题是 CircleView,当我显示 TableView 时,每个圆圈都有相同的颜色。我哪里错了?
更新
我按照提示修改了代码如下:
- (UITableViewCell *)tableView:(UITableView *)tableView cellForRowAtIndexPath:(NSIndexPath *)indexPath{
static NSString *MyIdentifier = @"MyReuseIdentifier";
EKCalendar *calendar;
calendar = [theCalendarSyncEngine.calendars objectAtIndex:indexPath.row];
CalendarCell *cell = [tableView dequeueReusableCellWithIdentifier:MyIdentifier];
if(cell==nil){
cell = [[CalendarCell alloc]initAndReuseIdentifier:MyIdentifier andIsChecked:FALSE];
}
cell.title.text=calendar.title;
//UPDATE METHOD TO SET COLOR
[cell setColor:calendar.CGColor];
return cell;
}
CalendarCell里面的方法:
- (void)setColor:(CGColorRef)color
{
[circle setColor:color];
[circle setNeedsDisplay];
}
它调用了 CircleView 方法:
- (void)setColor:(CGColorRef)color
{
thisColor=color;
}