6

右上隅に画像を描画するサブクラス化されたUITableViewCellを作成しようとしています。self.backgroundViewを設定した場合を除いて、完全に機能しています。背景画像は、drawRectで描画された画像を覆っています。

drawRectで行われていることを隠さずに、背景画像(およびselectedBackgroundView)を設定できる方法が必要です。

私はこれを間違った方法で行っていますか?

編集:問題のあるサンプルプロジェクトを投稿しました。

    - (id)initWithStyle:(UITableViewCellStyle)style reuseIdentifier:(NSString *)reuseIdentifier {

    if ((self = [super initWithStyle:style reuseIdentifier:reuseIdentifier])) {

     // TODO: figure out why this covers up self.starImage that's drawn in drawRect
         self.backgroundView = [[[UIImageView alloc] initWithImage:[UIImage imageNamed:@"cellBackground.png"]] autorelease];
    }
    return self;
}

- (void)drawRect:(CGRect)rect {
    [self.starImage drawAtPoint:CGPointMake(self.bounds.size.width - self.starImage.size.width, 0.0)];
}

編集2:AWrightIVの要求で、これが私がそれを機能させる方法です...これはUITableViewCellのサブクラス化をまったく必要としませんでした。cell.backgroundViewにサブビューを追加しているだけです。

// create a UIImageView that contains the background image for the cell
UIImageView *bgImageView = [[UIImageView alloc] initWithImage:[UIImage imageNamed:@"cellBackground.png"]];

// create another UIImageView that contains the corner image
UIImage *starRedImage = [UIImage imageNamed:@"starcorner_red.png"];
UIImageView *starImageView = [[UIImageView alloc] initWithFrame:CGRectMake(297,
                                                                               0,
                                                                               starRedImage.size.width,
                                                                               starRedImage.size.height)];
starImageView.image = starRedImage;

// add the corner UIImageView as a subview to the background UIImageView
[bgImageView addSubview:starImageView];

// set cell.background to use the background UIImageView
cell.backgroundView = bgImageView;
4

4 に答える 4

9

図面をそのようにセルと混合することは実際には想定されていません。UITableViewCell機構が動作しているよりも低いレベルで動作しているため、この問題が発生します。

これは、最終的に発生するさまざまな問題の1つにすぎません。その道を進むと、選択の仕組みの問題など、他の問題が発生します。

適切なアプローチは、描画するコードを含むカスタムUIViewを作成し、それをセルのルートビューにaddSubViewすることです。これにより、適切な順序でレンダリングが処理され、選択システムに干渉することはなく、この場合は正しく機能します。

于 2010-08-20T04:06:21.957 に答える
4

テーブルセルの-drawRect:をオーバーライドしないでください。代わりに、新しいカスタムビューを作成し、それをセルのcontentViewに追加して、そこに描画します。

于 2010-08-20T04:00:46.317 に答える
1

そこに追加してみました[super drawRect:rect];か?

于 2010-08-20T03:23:45.430 に答える
-2

これは少し厄介な解決策ですが、私の要件に正確に適合しています... 1つの致命的な欠陥があります。セルが再利用されると、不要なときにスターコーナーが表示されます。

http://dl.dropbox.com/u/2349787/UIImage_Position_subclassed_cell2.zip

ここではまだdrawRectを使用していますが、initWithStyleメソッド内でself.starImageにアクセスした場合にself.starImageがnullになるためです。また、サブビューをself.contentViewに追加する代わりに、セルの削除ボタンが干渉しないように、サブビューをself.backgroundViewに追加しています。スターコーナーは、ポートレートモードとランドスケープモードの両方で正しく配置され、編集モードでも正常に機能します。

ただし、セルの再利用の問題がありますが、それでも問題はありません...したがって、UITableViewCellをサブクラス化せずにそれを実行しようとしているのかもしれません。

私はさらなる提案を受け入れています。ありがとうございました!

- (id)initWithStyle:(UITableViewCellStyle)style reuseIdentifier:(NSString *)reuseIdentifier {
    if ((self = [super initWithStyle:style reuseIdentifier:reuseIdentifier])) {
        // Initialization code

        self.backgroundView = [[[UIImageView alloc] initWithImage:[UIImage imageNamed:@"cellBackground.png"]] autorelease];
    }
    return self;
}

- (void) drawRect:(CGRect)rect {

    UIImageView *imageView = [[[UIImageView alloc] initWithFrame:CGRectMake(self.bounds.size.width - self.starImage.size.width, 0, self.starImage.size.width, self.starImage.size.height)] autorelease];
    imageView.image = self.starImage;
    imageView.autoresizingMask = UIViewAutoresizingFlexibleLeftMargin;

    [self.backgroundView addSubview:imageView];
}
于 2010-08-20T12:36:32.817 に答える