私は iPhone アプリケーションを開発しています。テーブル ビューで、セルの選択スタイルにカスタム カラーが必要でした。UITableViewCell クラス リファレンスを読みましたが、選択スタイルに定義されている定数は 3 つしかありません (青、グレー、なし)。リファレンスで定義されている色とは異なる色を使用するアプリケーションを 1 つ見ました。
リファレンスで定義されている以外の色をどのように使用できますか?
前もって感謝します。
私は iPhone アプリケーションを開発しています。テーブル ビューで、セルの選択スタイルにカスタム カラーが必要でした。UITableViewCell クラス リファレンスを読みましたが、選択スタイルに定義されている定数は 3 つしかありません (青、グレー、なし)。リファレンスで定義されている色とは異なる色を使用するアプリケーションを 1 つ見ました。
リファレンスで定義されている以外の色をどのように使用できますか?
前もって感謝します。
選択を設定する最良の方法は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
できます。
行が選択されると、この背景が自動的に適用されます。
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
上記のいくつかを試しましたが、実際には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メソッドは無視してください。
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
}
選択が解除されて要件を満たすときに、このビューをアニメーション化することを選択できます。
サブクラス 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];
}
- (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];
}
}