具有Core图形的自定义表格视图单元格

时间:2013-10-24 08:32:25

标签: ios objective-c uitableview calendar core-graphics

我正在创建一个表格视图,用户可以在其中选择应用程序应在其中导入的日历,我的目的是制作类似于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 init方法:

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

1 个答案:

答案 0 :(得分:0)

问题是您在init方法中设置单元格的颜色。这意味着如果重复使用您的单元格,颜色将是创建单元格期间的颜色。

您应该将颜色移动到以下的块

if(cell==nil){
cell = [[CalendarCell alloc]initAndReuseIdentifier:MyIdentifier andIsChecked:FALSE andAColor:calendar.CGColor];}

在自定义单元格中创建一个设置颜色方法。并致电cell.setColor()calendar.CGColor

此外,您可能需要在circleView上调用setNeedDisplay以强制重新调用重用的单元格。