2

cellForRowAtIndexPath メソッドで UITableViewCell の selectedBackgroundView を適切に設定するのに苦労しています。私はいくつかのコードバリエーションを試しました...

CGRect customFrame = CGRectMake(cell.frame.origin.x + 1.0f, cell.frame.origin.y,
cell.frame.size.width - 22.0f, cell.frame.size.height);

UIView *cellBackground = [[[UIView alloc] initWithFrame:customFrame] autorelease];
cellBackground.backgroundColor = [UIColor colorWith8BitRed:0 green:0 blue:0 alpha:0.1f];
cell.selectedBackgroundView = cellBackground;

このスニペットでは、セルは適切な色の変化を示しますが、フレームの寸法は指定したものではありません (customFrame で初期化するようにビューを指定したにもかかわらず、それらは cell.frame の寸法のようです)

もう 1 つの方法を試してみました。子サブビューを持つ親ビューを作成することです。親ビューのフレームはセルと同じ寸法で、子サブビューのフレームには私の customFrame 寸法が含まれています。親ビューを backgroundView として設定すると、フレーム customFrame の寸法が適用されます。ただし、親ビューを selectedBackgroundView として設定しようとすると、セルを選択してもビューが表示されません。

CGRect customFrame = CGRectMake(cell.frame.origin.x + 1.0f, cell.frame.origin.y, 
cell.frame.size.width - 22.0f, cell.frame.size.height);

UIView *parentView = [[[UIView alloc] initWithFrame:cell.frame] autorelease]; 
UIView *childView = [[[UIView alloc] initWithFrame: customFrame] autorelease];

[parentView addSubview:childView];
childView.backgroundColor = [UIColor colorWith8BitRed:0 green:0 blue:0 alpha:0.1f];
cell.selectedBackgroundView = parentView;

selectedBackgroundView のフレーム サイズをカスタマイズするのはかなり難しいようです。どんな助けでも大歓迎です。ありがとう!

4

1 に答える 1

12

カスタムTableViewCellでこれが必要です

- (void)layoutSubviews
{
    [super layoutSubviews];
    self.selectedBackgroundView.frame = CGRectMake(cell.frame.origin.x + 1.0f, cell.frame.origin.y, cell.frame.size.width - 22.0f, cell.frame.size.height);
}

私は頭を掻かなければなりませんでした。なんらかの方法で 2 倍のフレームが必要になるため、それはちょっとばかげています。もちろんdefine、それ自体にプロパティを使用することもできますUIViewが、それでも...

Ortwin Gentzのおかげでそれを見つけることができました — 彼に小道具を!

于 2013-04-26T21:07:39.457 に答える