質問

I need to draw a circle in some UICollectionViewCell. Circle with different colored border and background color. My code.

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);

}

When scrolling, the circle is drawn on different cells. cell.titleLabel is always displayed correctly. Why a circle drawn in the cells where it should not be?

役に立ちましたか?

解決

The reason cell.titleLabel is always displayed correctly but the custom drawing isn't, is because you are updating the cell.titleLabel each time but the way you have the custom drawing setup, it will only effect newly created cells.

This is because under normal conditions, drawRect: is usually triggered when the view is first added to the screen and made visible. You are reusing cells, thus causing the drawing to remain on the views even when they are being used else where.

You need to add [cell setNeedsDisplay] after you change the cell.drawCircle value. This will cause the cell's drawRect: method to be triggered again.

ライセンス: CC-BY-SA帰属
所属していません StackOverflow
scroll top