0

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

1 回答 1

0

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

您应该将颜色集移到以下块之外

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

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

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

于 2013-10-24T08:49:52.260 回答