6

そのため、カスタム selectedBackgroundView を my 内に設定する際に問題に直面していUITableViewCellます。

私のセルには、contentView基本的にUIView(frame = 0,0,80,70) で、背景が黒UIImageViewでサブビューとして があります。

imageView のcontentMode = UIViewContentModeScaleAspectFit;

これは次のようになります。

選択されていないセル

ここで、selectedBackgroundView を次のように設定します。

    //set the custom selected color
    UIView *bgColorView                 = [[UIView alloc] init];
    bgColorView.backgroundColor         = MY_TINT_COLOR;
    CGColorRef darkColor                = [UIColor colorWithRed:0.0 green:0.0 blue:0.0 alpha: 0.25].CGColor;
    CGColorRef lightColor               = [self.view.backgroundColor colorWithAlphaComponent:0.0].CGColor;
    //setting some gradients here
    //should not be relevant for the question
    [cell setSelectedBackgroundView:bgColorView];

これにより、次のような結果になります。

ここに画像の説明を入力

私の質問は、なぜ がselectedBackgroundViewの黒い部分を隠すのcontentViewかということです。

bgColorViewから始まるフレームですでに初期化を試みましx = 80たが、何も変わりません。

backgroundColorまた、明示的にを黒に設定しようとしましたがimageView、同じ結果です。

この動作の原因は何ですか?

4

2 に答える 2

23

次の図は、セル構造のアイデアを示しています。選択した背景ビューはコンテンツ ビューを非表示にします

ここに画像の説明を入力

したがって、セルの背景色を黒に設定して試すことができます。カスタムセルは次のようになります

    self.backgroundColor = [UIColor blackColor]; // Gives black background for you


    // Set selected background view
    UIView *backgroundView = [[UIView alloc]initWithFrame:self.bounds];
    backgroundView.layer.borderColor = [[UIColor colorWithRed:0.529 green:0.808 blue:0.922 alpha:1]CGColor];
    backgroundView.layer.borderWidth = 10.0f;
    self.selectedBackgroundView = backgroundView;
    [backgroundView release];

    // Set the content view
    CGRect frame  = CGRectMake(self.bounds.origin.x+5, self.bounds.origin.y+5, self.bounds.size.width-10, self.bounds.size.height-10);
    UIImageView *imageView = [[UIImageView alloc] initWithFrame:frame];
    self.imageView = imageView;
    [imageView release];
    self.imageView.contentMode = UIViewContentModeScaleAspectFill ;
    self.imageView.clipsToBounds = YES;
    [self.contentView addSubview:self.imageView];
于 2013-03-11T12:17:54.103 に答える
0

オーバーライドを試すことができるかもしれません

- (void)setHighlighted:(BOOL)highlighted animated:(BOOL)animated

- (void)setSelected:(BOOL)selected animated:(BOOL)animated

目的の動作を取得するための UITableViewCell クラスの。

于 2013-03-11T12:06:50.840 に答える