35

私は iPhone アプリケーションを開発しています。テーブル ビューで、セルの選択スタイルにカスタム カラーが必要でした。UITableViewCell クラス リファレンスを読みましたが、選択スタイルに定義されている定数は 3 つしかありません (青、グレー、なし)。リファレンスで定義されている色とは異なる色を使用するアプリケーションを 1 つ見ました。

リファレンスで定義されている以外の色をどのように使用できますか?

前もって感謝します。

4

9 に答える 9

64

選択を設定する最良の方法はselectedBackgroundView、セルを作成するときにセルに を設定することです。

すなわち

- (UITableViewCell *)tableView:(UITableView *)tableView cellForRowAtIndexPath:(NSIndexPath *)indexPath {

    static NSString *CellIdentifier = @"Cell";
    UITableViewCell *cell = [tableView dequeueReusableCellWithIdentifier:CellIdentifier];
    if (cell == nil) {
        cell = [[[UITableViewCell alloc] initWithFrame:CGRectZero reuseIdentifier:CellIdentifier] autorelease];
        cell.selectedBackgroundView = [[[UIImageView alloc] initWithImage:[UIImage imageNamed:@"SelectedCellBackground.png"]] autorelease];
    }

    // configure the cell
}

使用する画像には、適切なグラデーションが必要です (デフォルトの選択のように)。フラットな色だけが必要な場合は、代わりに UIView を使用して、 を必要な色にUIImageView設定backgroundColorできます。

行が選択されると、この背景が自動的に適用されます。

于 2009-04-16T23:52:28.577 に答える
13

UITableViewCell をサブクラス化した場合は、以下をオーバーライドしてセルのさまざまな要素をカスタマイズできます。

- (void)setHighlighted:(BOOL)highlighted animated:(BOOL)animated {
    if(highlighted) {
        self.backgroundColor = [UIColor redColor];
    } else {
        self.backgroundColor = [UIColor clearColor];
    }

    [super setHighlighted:highlighted animated:animated];
}

iOS7の編集:Sashoが述べたように、あなたも必要です

cell.selectionStyle = UITableViewCellSelectionStyleNone
于 2012-07-24T14:34:33.493 に答える
7

上記のいくつかを試しましたが、実際にはUITableViewCellの独自のサブクラスを作成してから、touchesBegan / touchesCancelled/touchesEndedメソッドをオーバーライドすることを好みます。これを行うには、セルのselectedBackgroundViewプロパティとhighlightedColorプロパティをすべて無視し、代わりに、上記のメソッドのいずれかが呼び出されるたびにこれらの色を手動で設定します。たとえば、セルの背景を緑でテキストを赤に設定する場合は、次のことを試してください(カスタムセルサブクラス内)。

- (void)touchesBegan:(NSSet *)touches withEvent:(UIEvent *)event
{
    //Set backgorund
    self.backgroundColor = [UIColor themeBlue];

    //Set text
    self.textLabel.textColor = [UIColor themeWhite];

    //Call super
    [super touchesBegan:touches withEvent:event];
}

これを機能させるには、以下を設定する必要があることに注意してください。

self.selectionStyle = UITableViewCellSelectionStyleNone;

それ以外の場合は、最初に現在の選択スタイルを取得します。

編集:touchesCancelledメソッドを使用して元のセルの色に戻すことをお勧めしますが、touchesEndedメソッドは無視してください。

于 2012-07-22T12:02:16.180 に答える
2

didSelectRowAtIndexPath:をオーバーライドし、選択した色のUIViewを描画して、セル内のUILabelの後ろに挿入します。私はそれを次のようにします:

UIView* selectedView; //inside your header

- (void)tableView:(UITableView *)tableView didSelectRowAtIndexPath:(NSIndexPath *)indexPath{

  UITableViewCell* cell = [tableView cellForRowAtIndexPath:indexPath];
  selectedView = [[UIView alloc] initWithFrame:[cell frame]];
  selectedView.backgroundColor = [UIColor greenColor]; //whatever

  [cell insertSubview:selectedView atIndex:0]; //tweak this as necessary
  [selectedView release]; //clean up

}

選択が解除されて要件を満たすときに、このビューをアニメーション化することを選択できます。

于 2008-11-04T02:56:11.427 に答える
1

サブクラス UITableViewCell とオーバーライドsetHighlighted:animated:

backgroundColor を設定することで、カスタム選択色の色を定義できます (WIllster の回答を参照)。

- (void)setHighlighted:(BOOL)highlighted animated:(BOOL)animated {
    if(highlighted) {
        self.backgroundColor = [UIColor redColor];
    } else {
        self.backgroundColor = [UIColor clearColor];
    }

    [super setHighlighted:highlighted animated:animated];
}

backgroundView プロパティを設定することで、カスタムの背景画像を定義できます。

- (void)setHighlighted:(BOOL)highlighted animated:(BOOL)animated {
    if( highlighted == YES )
        self.backgroundView = [[UIImageView alloc] initWithImage:[UIImage imageNamed:@"seasonal_list_event_bar_default.png"]];
    else
        self.backgroundView = [[UIImageView alloc] initWithImage:[UIImage imageNamed:@"seasonal_list_event_bar_active.png"]];


    [super setHighlighted:highlighted animated:animated];
}
于 2012-10-25T14:31:56.260 に答える
0
- (void)setHighlighted:(BOOL)highlighted animated:(BOOL)animated {  
// Set Highlighted Color  
if (highlighted) {  
self.backgroundColor = [UIColor colorWithRed:234.0f/255 green:202.0f/255 blue:255.0f/255 alpha:1.0f];
    } else {  
        self.backgroundColor = [UIColor clearColor];  
  }   
}
于 2013-07-04T11:59:11.007 に答える