How to change color of UITableViewCell when selecting?

前端 未结 14 2484
予麋鹿
予麋鹿 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:30

    I end up with following code.

    - (UITableViewCell *)tableView:(UITableView *)tableView cellForRowAtIndexPath:(NSIndexPath *)indexPath {
    
      //  UITableViewCell *cell = [tableView dequeueReusableCellWithIdentifier:[cellIdArray objectAtIndex:indexPath.row] forIndexPath:indexPath];
    
        // Configure the cell...
        cell.backgroundView =
        [[UIImageView alloc] init] ;
        cell.selectedBackgroundView =[[UIImageView alloc] init];
    
        UIImage *rowBackground;
        UIImage *selectionBackground;
    
    
        rowBackground = [UIImage imageNamed:@"cellBackgroundDarkGrey.png"];
        selectionBackground = [UIImage imageNamed:@"selectedMenu.png"];
    
        ((UIImageView *)cell.backgroundView).image = rowBackground;
        ((UIImageView *)cell.selectedBackgroundView).image = selectionBackground;
    
    
    
        return cell;
    }
    
    0 讨论(0)
  • 2020-12-04 11:35

    Add following code to cellForRowAtIndexPath method

    var cell=tableView.dequeueReusableCellWithIdentifier("cell")!
    var viewBG=UIView(frame: CGRectMake(0,0,self.view.frame.size.width,50))
    viewBG.backgroundColor=UIColor(colorLiteralRed: 71.0/255.0, green: 121.0/255.0, blue: 172.0/255.0, alpha: 1)
    cell.selectedBackgroundView=viewBG
    
    0 讨论(0)
  • 2020-12-04 11:37

    You could create a custom UITableViewCell, in which you add a UIButton with the size of the cell. Then you can easily make custom methods for the UIButton's TouchDown (hover) and TouchUpInside events, and set the background.

    - (id)initWithStyle:(UITableViewCellStyle)style reuseIdentifier:(NSString *)reuseIdentifier
    {
        self = [super initWithStyle:style reuseIdentifier:reuseIdentifier];
        if (self) {
            cellButton = [[UIButton alloc] initWithFrame:self.contentView.frame];
            UIImage *cellBackgroundNormal = [UIImage imageNamed:@"cell_menu_normal"];
            UIImageView *cellBackgroundView = [[UIImageView alloc] initWithImage:cellBackgroundNormal];
            self.backgroundView = cellBackgroundView;
    
            [cellButton addTarget:self action:@selector(hoverCell) forControlEvents:UIControlEventTouchDown];
            [cellButton addTarget:self action:@selector(tapCell) forControlEvents:UIControlEventTouchUpInside];
        }
        return self;
    }
    
    - (void)hoverCell
    {
        UIImage *cellBackgroundHover = [UIImage imageNamed:@"cell_menu_third_image"];
        UIImageView *cellBackgroundHoverView = [[UIImageView alloc] initWithImage:cellBackgroundHover];
        self.backgroundView = cellBackgroundHoverView;
    }
    
    - (void)tapCell
    {
        UIImage *cellBackgroundSelected = [UIImage imageNamed:@"cell_menu_selected"];
        UIImageView *cellBackgroundSelectedView = [[UIImageView alloc] initWithImage:cellBackgroundSelected];
        self.backgroundView = cellBackgroundSelectedView;
    }
    
    0 讨论(0)
  • 2020-12-04 11:39

    Based on Milan Cermak's answer, you can use UIAppearance.

    In Swift 1.1 / 2.0:

    let selectionView = UIView()
    selectionView.backgroundColor = UIColor.redColor()
    UITableViewCell.appearance().selectedBackgroundView = selectionView
    
    0 讨论(0)
  • 2020-12-04 11:40

    You can also use UIAppearance like this:

    UIView *selectionView = [UIView new];
    selectionView.backgroundColor = [UIColor redColor];
    [[UITableViewCell appearance] setSelectedBackgroundView:selectionView];
    

    This will apply to all instances of UITableViewCell or any of its subclasses you might have. Just make sure the selectionStyle property of your cell is not set to UITableViewCellSelectionStyleNone.

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

    Swift:

    override func tableView(tableView: UITableView, didSelectRowAtIndexPath indexPath: NSIndexPath) {
        tableView.deselectRowAtIndexPath(indexPath, animated: true)
    }
    
    override func tableView(tableView: UITableView, willDisplayCell cell: UITableViewCell, forRowAtIndexPath indexPath: NSIndexPath) {
        let selectionColor = UIView() as UIView
        selectionColor.layer.borderWidth = 1
        selectionColor.layer.borderColor = UIColor.blueColor().CGColor
        selectionColor.backgroundColor = UIColor.blueColor()
        cell.selectedBackgroundView = selectionColor
    }
    

    Swift 4:

    override func tableView(_ tableView: UITableView, didSelectRowAt indexPath: IndexPath)
    {
        tableView.deselectRow(at: indexPath, animated: true)
    }
    
    override func tableView(_ tableView: UITableView, willDisplay cell: UITableViewCell, forRowAt indexPath: IndexPath)
    {
        let selectionColor = UIView() as UIView
        selectionColor.layer.borderWidth = 1
        selectionColor.layer.borderColor = UIColor.blue.cgColor
        selectionColor.backgroundColor = UIColor.blue
        cell.selectedBackgroundView = selectionColor
    }
    
    0 讨论(0)
提交回复
热议问题