首页
学习
活动
专区
圈层
工具
发布
社区首页 >问答首页 >UICollectionViewCell drawRect:问题

UICollectionViewCell drawRect:问题
EN

Stack Overflow用户
提问于 2014-02-11 21:53:13
回答 1查看 3.1K关注 0票数 2

我需要在UICollectionViewCell中画一个圆圈。有不同颜色的边框和背景色的圆圈。我的密码。

UICollectionViewController

代码语言:javascript
复制
- (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

代码语言:javascript
复制
- (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总是正确显示。为什么画在牢房里的圆圈不应该在哪里?

EN

回答 1

Stack Overflow用户

回答已采纳

发布于 2014-02-11 22:18:42

cell.titleLabel总是正确显示但自定义绘图没有显示的原因,是因为每次都要更新cell.titleLabel,但是按照自定义绘图设置的方式,它只会影响新创建的单元格。

这是因为在正常情况下,drawRect:通常是在视图首次添加到屏幕并使其可见时触发的。您正在重用单元格,从而导致绘图保留在视图上,即使在其他地方使用时也是如此。

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

票数 5
EN
页面原文内容由Stack Overflow提供。腾讯云小微IT领域专用引擎提供翻译支持
原文链接:

https://stackoverflow.com/questions/21713861

复制
相关文章

相似问题

领券
问题归档专栏文章快讯文章归档关键词归档开发者手册归档开发者手册 Section 归档