3

その中UITableViewにカスタムセルがあります。

drawRectメソッドを使用してセルを自分でレンダリングしたい(マスクを使用して角の丸い画像をレンダリングしています)。

とにかく、drawRecttableView のメソッドを使用すると、セルが 1 つだけ描画されます。残りはすべてそこにあり、目に見えないだけです。タップできますが、表示されません。

メソッドを削除するとdrawRect、すべてのセルが表示され、ラベルが表示されます。

そのdrawRect方法は…

- (void)drawRect:(CGRect)rect
{
    CGContextRef context = UIGraphicsGetCurrentContext();

    CGRect mask = CGRectMake(10, 10, rect.size.width - 20, rect.size.height - 20);

    UIBezierPath *maskPath = [UIBezierPath bezierPathWithRoundedRect:mask cornerRadius:4];

    CAShapeLayer *maskLayer = [CAShapeLayer layer];
    maskLayer.frame = self.frame;

    maskLayer.path = maskPath.CGPath;

    self.layer.mask = maskLayer;

    if (self.image == nil) {
        CGContextSetFillColorWithColor(context, [UIColor colorWithWhite:0.6 alpha:1.0].CGColor);
        CGContextFillRect(context, rect);
    } else {
        float width = rect.size.width - 20;
        float height = self.image.size.height / self.image.size.width * width;

        CGRect imageRect = CGRectMake((rect.size.width - width) * 0.5, (rect.size.height - height) * 0.5, width, height);
        [self.image drawInRect:imageRect];
    }
}

ここで何か間違ったことをしていますか?

また、マスクを削除すると、すべてのセルが描画されますが、丸みを帯びた角のためにそこにマスクが必要です。

編集 - 画像描画の削除

drawRect メソッドを編集しました (これは にありますUITableViewCell) (なぜ tableView に入れるのでしょうか?)

とにかく、新しいdrawRect方法は...

- (void)drawRect:(CGRect)rect
{
    if (self.image == nil) {
        CGContextRef context = UIGraphicsGetCurrentContext();

        CGRect mask = CGRectMake(10, 10, rect.size.width - 20, rect.size.height - 20);

        UIBezierPath *maskPath = [UIBezierPath bezierPathWithRoundedRect:mask cornerRadius:4];

        CAShapeLayer *maskLayer = [CAShapeLayer layer];
        maskLayer.frame = self.frame;

        maskLayer.path = maskPath.CGPath;

        self.layer.mask = maskLayer;

        CGContextSetFillColorWithColor(context, [UIColor colorWithWhite:0.7 alpha:1.0].CGColor);
        CGContextFillRect(context, rect);
    }
}

画像が nil の場合、その場所に灰色の長方形がレンダリングされます。ただし、最初のセルのみが表示されます。マスクにより、他のセルが表示されないように見えます。

4

1 に答える 1