58

選択したセルごとに、UITableViewCell を使用してセルの背景色を変更する方法を知っている人はいますか? TableView のコード内でこの UITableViewCell を作成しました。

4

30 に答える 30

95

プロパティ 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];
于 2010-07-05T04:45:00.803 に答える
39

スタイルがグループ化されたテーブルビューでこれを機能させることができました。

まず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];
    }
}
于 2012-02-17T08:25:10.140 に答える
7

選択したセルについて話している場合、プロパティは-selectedBackgroundViewです。これは、ユーザーがセルを選択したときに表示されます。

于 2010-03-10T16:02:49.690 に答える
6

高度にカスタマイズされた 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];
于 2013-01-14T13:55:19.917 に答える
6

UITableViewCellsetSelected: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];

お役に立てれば。:)

于 2014-07-14T16:06:40.610 に答える
6

私は次のことで運が良かった:

- (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];
    }
}
于 2010-03-10T16:27:36.700 に答える
5

灰色の背景色を削除したいだけの場合は、次のようにします。

-(void)tableView:(UITableView *)tableView didSelectRowAtIndexPath:(NSIndexPath *)indexPath
{
     [[tableView cellForRowAtIndexPath:indexPath] setSelectionStyle:UITableViewCellSelectionStyleNone];
}     
于 2013-10-27T12:18:05.673 に答える
4

デフォルトのスタイルは灰色で、プログラムで実行された場合、セルの色が破壊されます。これを回避するためにこれを行うことができます。(スイフトで)
cell.selectionStyle = .None

于 2015-09-08T04:16:07.200 に答える
3

私のために働く

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;
于 2016-05-17T07:23:45.157 に答える
3

Apple のサンプル コードAdvancedTableViewCellsで確認してください。

複合セル パターンを使用する必要があります。

于 2011-04-20T08:58:19.830 に答える
1

サブクラス化してデフォルトを使用して色を設定することによりUIAppearanceiOS 7 (およびそれ以降?)で(適切に)機能するソリューションについては、同様の質問に対する私の回答をご覧くださいUITableViewCellselectedBackgroundView

于 2015-08-26T10:15:12.550 に答える
0

上記の回答をそれぞれ試しましたが、どれも私に最適ではありませんでした。

次に、ネイティブで提供されているメソッドの 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;
}

他のすべてに対するこの方法の利点は次のとおりです。

  1. 複数のセル選択で機能します
  2. 特定のセルが選択されたときと選択解除されたときの背景色だけでなく、必要に応じて任意の要素を変更できます。
于 2016-11-22T12:57:49.697 に答える
0

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)
}
于 2020-07-28T05:29:29.713 に答える