選択したセルごとに、UITableViewCell を使用してセルの背景色を変更する方法を知っている人はいますか? TableView のコード内でこの UITableViewCell を作成しました。
30 に答える
プロパティ selectedBackgroundView を変更するのが正しく、最も簡単な方法です。次のコードを使用して、選択色を変更します。
// set selection color
UIView *myBackView = [[UIView alloc] initWithFrame:cell.frame];
myBackView.backgroundColor = [UIColor colorWithRed:1 green:1 blue:0.75 alpha:1];
cell.selectedBackgroundView = myBackView;
[myBackView release];
スタイルがグループ化されたテーブルビューでこれを機能させることができました。
まずselectionStyle
、すべてのセルのプロパティを に設定しますUITableViewCellSelectionStyleNone
。
cell.selectionStyle = UITableViewCellSelectionStyleNone;
次に、テーブル ビュー デリゲートに以下を実装します。
static NSColor *SelectedCellBGColor = ...;
static NSColor *NotSelectedCellBGColor = ...;
- (NSIndexPath *)tableView:(UITableView *)tableView willSelectRowAtIndexPath:(NSIndexPath *)indexPath
{
NSIndexPath *currentSelectedIndexPath = [tableView indexPathForSelectedRow];
if (currentSelectedIndexPath != nil)
{
[[tableView cellForRowAtIndexPath:currentSelectedIndexPath] setBackgroundColor:NotSelectedCellBGColor];
}
return indexPath;
}
- (void)tableView:(UITableView *)tableView didSelectRowAtIndexPath:(NSIndexPath *)indexPath
{
[[tableView cellForRowAtIndexPath:indexPath] setBackgroundColor:SelectedCellBGColor];
}
- (void)tableView:(UITableView *)tableView willDisplayCell:(UITableViewCell *)cell forRowAtIndexPath:(NSIndexPath *)indexPath
{
if (cell.isSelected == YES)
{
[cell setBackgroundColor:SelectedCellBGColor];
}
else
{
[cell setBackgroundColor:NotSelectedCellBGColor];
}
}
選択したセルについて話している場合、プロパティは-selectedBackgroundView
です。これは、ユーザーがセルを選択したときに表示されます。
高度にカスタマイズされた UITableViewCell があります。そこで、独自のセル選択を実装しました。
cell.selectionStyle = UITableViewCellSelectionStyleNone;
セルのクラスにメソッドを作成しました:
- (void)highlightCell:(BOOL)highlight
{
if (highlight) {
self.contentView.backgroundColor = RGB(0x355881);
_bodyLabel.textColor = RGB(0xffffff);
_fromLabel.textColor = RGB(0xffffff);
_subjectLabel.textColor = RGB(0xffffff);
_dateLabel.textColor = RGB(0xffffff);
}
else {
self.contentView.backgroundColor = RGB(0xf7f7f7);;
_bodyLabel.textColor = RGB(0xaaaaaa);
_fromLabel.textColor = [UIColor blackColor];
_subjectLabel.textColor = [UIColor blackColor];
_dateLabel.textColor = RGB(0x496487);
}
}
ViewWillAppear の私の UITableViewController クラスにこれを追加しました:
NSIndexPath *tableSelection = [self.tableView indexPathForSelectedRow];
SideSwipeTableViewCell *cell = (SideSwipeTableViewCell*)[self.tableView cellForRowAtIndexPath:tableSelection];
[cell highlightCell:NO];
didSelectRow でこれを追加しました:
SideSwipeTableViewCell *cell = (SideSwipeTableViewCell*)[self.tableView cellForRowAtIndexPath:indexPath];
[cell highlightCell:YES];
UITableViewCell
setSelected:animated: メソッドのサブクラスを作成して実装することで、この問題を解決できました。
- (void)setSelected:(BOOL)selected animated:(BOOL)animated {
[super setSelected:selected animated:animated];
// Configure the view for the selected state
if(selected) {
[self setSelectionStyle:UITableViewCellSelectionStyleNone];
[self setBackgroundColor:[UIColor greenColor]];
} else {
[self setBackgroundColor:[UIColor whiteColor]];
}
}
トリックは
cell.selectionStyle = UITableViewCellSelectionStyleDefault;
実装ビューコントローラーで、次にtableViewCellで次のように設定します
[self setSelectionStyle:UITableViewCellSelectionStyleNone];
お役に立てれば。:)
私は次のことで運が良かった:
- (void)tableView:(UITableView *)tableView willDisplayCell:(UITableViewCell *)cell forRowAtIndexPath:(NSIndexPath *)indexPath
{
bool isSelected = // enter your own code here
if (isSelected)
{
[cell setBackgroundColor:[UIColor colorWithRed:1 green:1 blue:0.75 alpha:1]];
[cell setAccessibilityTraits:UIAccessibilityTraitSelected];
}
else
{
[cell setBackgroundColor:[UIColor clearColor]];
[cell setAccessibilityTraits:0];
}
}
灰色の背景色を削除したいだけの場合は、次のようにします。
-(void)tableView:(UITableView *)tableView didSelectRowAtIndexPath:(NSIndexPath *)indexPath
{
[[tableView cellForRowAtIndexPath:indexPath] setSelectionStyle:UITableViewCellSelectionStyleNone];
}
デフォルトのスタイルは灰色で、プログラムで実行された場合、セルの色が破壊されます。これを回避するためにこれを行うことができます。(スイフトで)
cell.selectionStyle = .None
私のために働く
UIView *customColorView = [[UIView alloc] init];
customColorView.backgroundColor = [UIColor colorWithRed:180/255.0
green:138/255.0
blue:171/255.0
alpha:0.5];
cell.selectedBackgroundView = customColorView;
Apple のサンプル コードAdvancedTableViewCells
で確認してください。
複合セル パターンを使用する必要があります。
サブクラス化してデフォルトを使用して色を設定することによりUIAppearance
、iOS 7 (およびそれ以降?)で(適切に)機能するソリューションについては、同様の質問に対する私の回答をご覧ください。UITableViewCell
selectedBackgroundView
上記の回答をそれぞれ試しましたが、どれも私に最適ではありませんでした。
次に、ネイティブで提供されているメソッドの 1 つを調べましたが、正常に動作しています。
まず、 cellSelectionStyle を None にしてから、このソリューションに進みます。
func tableView(_ tableView: UITableView, willDeselectRowAt indexPath: IndexPath) -> IndexPath?
{
let cell = tableView.cellForRow(at: indexPath);
//cell which is getting deselected, make whatever changes that are required to make it back normal
cell.backgroundColor = kNormalColor;
return indexPath;
}
func tableView(_ tableView: UITableView, willSelectRowAt indexPath: IndexPath) -> IndexPath?
{
let cell = tableView.cellForRow(at: indexPath);
//cell which is getting selected, make whatever changes that are required to make it selected
cell.backgroundColor = kSelectedColor;
return indexPath;
}
他のすべてに対するこの方法の利点は次のとおりです。
- 複数のセル選択で機能します
- 特定のセルが選択されたときと選択解除されたときの背景色だけでなく、必要に応じて任意の要素を変更できます。
Swift 5 の更新で、テーブル ビューがフラッシュ選択され、選択されたセルの選択が解除されるという最近の問題がありました。ここでいくつかのソリューションを試しましたが、どれもうまくいきませんでした。解決策はfalseに設定しています。clearsSelectionOnViewWillAppear
以前は UIView と selectedBackgroundColor プロパティを使用していたので、そのアプローチを維持しました。
override func tableView(_ tableView: UITableView, cellForRowAt indexPath: IndexPath) -> UITableViewCell {
let cell = tableView.dequeueReusableCell(withIdentifier: "popoverCell", for: indexPath) as! PopoverCell
let backgroundView = UIView()
backgroundView.backgroundColor = Color.Blue
cell.selectedBackgroundView = backgroundView
}
以下は、Swift 5 に必要な変更です。このプロパティclearsSelectionOnViewWillAppear
が、セルの選択が解除された理由でした。最初のロードでは、次の選択が必要でした。
override func viewDidLoad() {
super.viewDidLoad()
clearsSelectionOnViewWillAppear = false
popoverTableView.selectRow(at: selectedIndexPath, animated: false, scrollPosition: .none)
}