8

StackOverflowで見つけたいくつかの回答を読んで試しました。私もブログからいくつかのことを読んで試しましたが、私が探していることを達成するものは何もないようです。

を作成し、UIViewその背景色を(標準の青または灰色の選択色ではなく)UITableViewCell希望の選択色に設定します。これをセルに追加すると、これは正常に機能します。ユーザーが選択すると、セルが目的の色に変わります。UIViewselectedBackgroundView

この方法はプレーンでうまく機能しUITableViewsます; Groupedではあまりうまくいきません。グループ化UITableViewされた場合、以下のスクリーンショットに示されているように、最初と最後のセルはクリップ/マスクの境界に準拠していません。

左上と右上の角だけを丸める方法はないことを私は知っています。

画像なしで、厳密にコードでこれを実行したいと思います。

質問

画像ではなく画像のみを使用してselectedBackgroundView色を変更し、最初と最後のセルを丸みを帯びた角の境界に一致させるためのちょっとした回避策を知っている人はいますか?UITableViewCellUIView

- (UITableViewCell *)tableView:(UITableView *)tableView cellForRowAtIndexPath:(NSIndexPath *)indexPath
{
    static NSString * CellIdentifier = @"Cell";
    WCSBadgedCell   * cell = [[WCSBadgedCell alloc] initWithStyle:UITableViewCellStyleSubtitle andBadgeStyle:0 reuseIdentifier:CellIdentifier]; 
    if (cell == nil) {
        cell = [[WCSBadgedCell alloc] initWithStyle:UITableViewCellStyleDefault andBadgeStyle:0 reuseIdentifier:CellIdentifier];
    }

    UIView *bgColorView = [[UIView alloc] init];
    [bgColorView setBackgroundColor:DARKBROWN];
    [bgColorView setClipsToBounds: YES];
    [cell.layer setMasksToBounds:YES];
    [cell setSelectedBackgroundView:bgColorView];

    [cell.textLabel setText: @"Testing a Cell"];

    return cell;
}

スクリーンショット

ここに画像の説明を入力してください

ここに画像の説明を入力してください

ここに画像の説明を入力してください

解決

私はCodaFisの回答を受け入れました。彼は、かなり良い(まだ長い)解決策を示すコメントを追加したからです。かなりの改良を行う必要がありましたが、最終的には、最初と最後のセルの角を曲がる、必要なselectedBackgroundViewができました。ありがとうございます。

これは私がこれをどのように達成したかの例です。

4

2 に答える 2

3

セルが複雑なため、UITableViewCellサブクラスを使用していると思います。これは私がそれをやってきた方法です:

- (id)initWithStyle:(UITableViewCellStyle)style reuseIdentifier:(NSString *)reuseIdentifier
{
    if ((self = [super initWithStyle:style reuseIdentifier:reuseIdentifier]))
    {
        self.clipsToBounds = YES;

        UIView* bgView = [[UIView alloc] init];
        bgView.backgroundColor = [UIColor colorWithWhite:0.f alpha:0.25f];
        self.selectedBackgroundView = bgView;
                //other code

    }
    return self;
}

これにより、必要な画像ではなく、セル上に一種の濃い灰色のオーバーレイが生成されます。

あなたの場合、選択したセルの正確な色(便利なダンディデジタルカラーメーターのおかげで)は次のようになります

[UIColor colorWithRed:106.0f/255.0f green:51.0f/255.0f blue:6.0f/255.0f alpha:1.0f];

白いテキストは

- (void)setSelected:(BOOL)sel animated:(BOOL)animated
{
    [super setSelected:sel animated:animated];

    if (sel)
    {
        self.textLabel.textColor = [UIColor whiteColor];

        }
    else
    {
        self.textLabel.textColor = [UIColor colorWithRed:(105.f/255.f) green:(50.f/255.f) blue:(6.f/255.f) alpha:1.f];  

    }
}
于 2012-04-18T23:43:13.780 に答える
1

-(void)setHighlighted:(BOOL)highlighted animated:(BOOL)animated同じ問題が発生し、UITableViewCellサブクラスでオーバーライドし、セルの作成時に選択スタイルを設定しないことで修正しました

//set no selection style in the cell
...
[self setSelectionStyle:UITableViewCellSelectionStyleNone];
...

//override setHighlighted to manually set the regular/highlighted background colors
-(void)setHighlighted:(BOOL)highlighted animated:(BOOL)animated {
    [super setHighlighted:highlighted animated:animated];
    if(highlighted){
        self.backgroundColor = [UIColor greenColor];
    }
    else {
        self.backgroundColor = [UIColor redColor];
    }
}
于 2012-12-12T18:50:29.093 に答える