我需要在UICollectionViewCell中画一个圆圈。有不同颜色的边框和背景色的圆圈。我的密码。
UICollectionViewController
- (UICollectionViewCell *)collectionView:(UICollectionView *)collectionView cellForItemAtIndexPath:(NSIndexPath *)indexPath
{
CalendarCell *cell;
firstDayInMonth = [self dayWeekStart:[self getDateFromItem:dateFromStart section:indexPath.section row:1]];
if (indexPath.row < firstDayInMonth) {
cell = [collectionView dequeueReusableCellWithReuseIdentifier:@"cellCalendarEmpty" forIndexPath:indexPath];
} else {
cell = [collectionView dequeueReusableCellWithReuseIdentifier:@"cellCalendar" forIndexPath:indexPath];
if ([self compareOnlyDate:[self getDateFromItem:dateFromStart section:indexPath.section row:indexPath.item-firstDayInMonth] date2:[self getDateLocaleTimeZone:[NSDate date]]] == 1) {
cell.bgColor = [UIColor blueColor];
cell.titleLabel.textColor = [UIColor whiteColor];
cell.drawCircle = [NSNumber numberWithInt:1];
toDaySection = indexPath.section;
} else {
cell.bgColor = [UIColor whiteColor];
cell.drawCircle = [NSNumber numberWithInt:0];
cell.titleLabel.textColor = [UIColor lightGrayColor];
}
cell.titleLabel.text = [NSString stringWithFormat:@"%i", indexPath.row-firstDayInMonth+1];
}
return cell;
}UICollectionViewCell
- (void)drawRect:(CGRect)rect
{
if ([self.drawCircle integerValue] == 1) {
[self drawCircl:0.0 end:0.5 color:[UIColor blueColor] bgColor:self.bgColor];
[self drawCircl:0.5 end:1.0 color:[UIColor redColor] bgColor:self.bgColor];
}
}
- (void) drawCircl:(float)start end:(float)end color:(UIColor*)color bgColor:(UIColor*)bgColor{
context = UIGraphicsGetCurrentContext();
CGContextSetLineWidth(context, 0.8);
CGFloat startAngle = start * 2 * M_PI - M_PI/2;
CGFloat endAngle = end * 2 * M_PI - M_PI/2;
CGContextAddArc(context, 15, 15, 14, startAngle, endAngle, NO);
CGContextSetFillColorWithColor(context, bgColor.CGColor);
CGContextSetStrokeColorWithColor(context, color.CGColor);
CGContextDrawPath(context, kCGPathFillStroke);
}滚动时,在不同的单元格上绘制圆圈。cell.titleLabel总是正确显示。为什么画在牢房里的圆圈不应该在哪里?
发布于 2014-02-11 22:18:42
cell.titleLabel总是正确显示但自定义绘图没有显示的原因,是因为每次都要更新cell.titleLabel,但是按照自定义绘图设置的方式,它只会影响新创建的单元格。
这是因为在正常情况下,drawRect:通常是在视图首次添加到屏幕并使其可见时触发的。您正在重用单元格,从而导致绘图保留在视图上,即使在其他地方使用时也是如此。
您需要在更改[cell setNeedsDisplay]值之后添加cell.drawCircle。这将导致再次触发单元格的drawRect:方法。
https://stackoverflow.com/questions/21713861
复制相似问题