How to change color of UITableViewCell when selecting?

前端 未结 14 2482
予麋鹿
予麋鹿 2020-12-04 10:59

I have a menu like so:

\"Menu\"

The normal (unselected) state for each cell is an image, the selected s

相关标签:
14条回答
  • 2020-12-04 11:18

    Easier than accepted answer:

    In your UITableViewCell subclass:

    In awakeFromNib or init:

    self.selectionStyle = UITableViewCellSelectionStyleNone;

    Then:

    - (void)setHighlighted:(BOOL)highlighted animated:(BOOL)animated
    {
        [super setHighlighted:highlighted animated:animated];
    
        if (highlighted) {
            self.backgroundColor = [UIColor yourHighlightColor];
        }
        else {
            self.backgroundColor = [UIColor yourNormalColor];
        }
    }
    
    0 讨论(0)
  • 2020-12-04 11:19

    iOS 6.0 and later

    - (BOOL)tableView:(UITableView *)tableView shouldHighlightRowAtIndexPath:(NSIndexPath *)indexPath {
    return YES;
    }
    
    - (void)tableView:(UITableView *)tableView didHighlightRowAtIndexPath:(NSIndexPath *)indexPath {
      // Add your Colour.
        CustomCell *cell = (CustomCell *)[tableView cellForRowAtIndexPath:indexPath];
        [self setCellColor:[UIColor whiteColor] ForCell:cell];  //highlight colour
    }
    
    - (void)tableView:(UITableView *)tableView didUnhighlightRowAtIndexPath:(NSIndexPath *)indexPath {
      // Reset Colour.
        CustomCell *cell = (CustomCell *)[tableView cellForRowAtIndexPath:indexPath];
        [self setCellColor:[UIColor colorWithWhite:0.961 alpha:1.000] ForCell:cell]; //normal color
    
    }
    
    - (void)setCellColor:(UIColor *)color ForCell:(UITableViewCell *)cell {
        cell.contentView.backgroundColor = color;
        cell.backgroundColor = color;
    }
    

    Custom UITableViewCell

    - (void)setSelected:(BOOL)selected animated:(BOOL)animated {
        [super setSelected:selected animated:animated];
    
        UIView * selectedBackgroundView = [[UIView alloc] init];
        [selectedBackgroundView setBackgroundColor:[UIColor colorFromHexString:@"5E6073"]]; // set color here
        [self setSelectedBackgroundView:selectedBackgroundView];
    }
    
    0 讨论(0)
  • 2020-12-04 11:21

    You could introduce a timer to set the time you want (like 1/2 s if I understood) between the 2 different background colors ? But you may already thought about that :/

    0 讨论(0)
  • 2020-12-04 11:25

    Try this in custom cell-

    - (void)awakeFromNib
    {
        UIView *selectedBackgroundView = [[UIView alloc] initWithFrame:self.bounds];
        selectedBackgroundView.autoresizingMask = UIViewAutoresizingFlexibleHeight | UIViewAutoresizingFlexibleWidth;
        selectedBackgroundView.backgroundColor = [UIColor colorWithRed:246.0/255.0 green:95.0/255.0 blue:22.0/255.0 alpha:1.0];
        self.selectedBackgroundView = selectedBackgroundView;
    }
    
    0 讨论(0)
  • 2020-12-04 11:27

    For swift 3

    self.tableView.reloadData()
    let selectedCell = tableView.cellForRow(at: indexPath)
    selectedCell?.contentView.backgroundColor = UIColor.red
    
    0 讨论(0)
  • 2020-12-04 11:28

    Custom UITableViewCell Swift 3.0

    override func awakeFromNib() {
            super.awakeFromNib()
    
            let selectedBackgroundView = UIView();
            selectedBackgroundView.backgroundColor = UIColor.lightGray;
            self.selectedBackgroundView = selectedBackgroundView;
        }
    
    0 讨论(0)
提交回复
热议问题