iphone - UICollectionViewCell绘制矩形: issues

标签 iphone objective-c drawrect uicollectionviewcell

我需要在一些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 始终正确显示。为什么在单元格不应该画的地方画了一个圆圈?

最佳答案

cell.titleLabel 始终正确显示但自定义绘图却无法正确显示的原因是,您每次都会更新 cell.titleLabel 但自定义绘图设置的方式会只影响新创建的细胞。

这是因为在正常情况下,当 View 第一次添加到屏幕并变得可见时,通常会触发drawRect:。您正在重复使用单元格,从而导致绘图保留在 View 上,即使它们在其他地方使用也是如此。

更改cell.drawCircle值后,您需要添加[cell setNeedsDisplay]。这将导致单元格的drawRect:方法再次被触发。

关于iphone - UICollectionViewCell绘制矩形: issues,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/21713861/

相关文章:

ios - XCode 7 和 iOS 8 自动布局顶部约束问题

ios - 在 iOS 上使用 midi 演奏打击乐器

ios - drawRect 不想画任何东西

ios UITableViewCell 以编程方式在 drawRect 与 layoutSubviews 中创建

iphone - 从 iOS 访问 Oracle

iphone - initWithArray 似乎给出了对源数组而不是新数组的引用

.net - 设计在多个移动平台上消费的服务

ios - 确定 UIImage 的主要颜色和次要颜色

ios - 隐藏UILabel而不占用任何空间

ios - UIGraphicsGetCurrentContext() 生命周期短