How do I set UITableViewCellSelectionStyle property to some custom color?

前端 未结 9 1729
栀梦
栀梦 2020-12-13 04:04

I am developing an iPhone application, in my table view I wanted custom color for Cell Selection Style, I read the UITableViewCell Class Reference but there are onl

相关标签:
9条回答
  • 2020-12-13 04:40

    Override didSelectRowAtIndexPath: and draw a UIView of a color of your choosing and insert it behind the UILabel inside the cell. I would do it something like this:

    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
    
    }
    

    You can choose to animate this view out when it gets deselected and will satisfy your requirements.

    0 讨论(0)
  • 2020-12-13 04:41
    - (BOOL)tableView:(UITableView *)tableView shouldHighlightRowAtIndexPath:(NSIndexPath *)indexPath {
        return YES;
    }
    
    - (void)tableView:(UITableView *)tableView didHighlightRowAtIndexPath:(NSIndexPath *)indexPath {
        // Add your Colour.
        SocialTableViewCell *cell = (SocialTableViewCell *)[tableView cellForRowAtIndexPath:indexPath];
        [self setCellColor:Ripple_Colour ForCell:cell];  //highlight colour
    }
    
    - (void)tableView:(UITableView *)tableView didUnhighlightRowAtIndexPath:(NSIndexPath *)indexPath {
        // Reset Colour.
        SocialTableViewCell *cell = (SocialTableViewCell *)[tableView cellForRowAtIndexPath:indexPath];
        [self setCellColor:Ripple_Colour ForCell:cell]; //normal color
    
    }
    
    - (void)setCellColor:(UIColor *)color ForCell:(UITableViewCell *)cell {
        cell.contentView.backgroundColor = color;
        cell.backgroundColor = color;
    }
    
    0 讨论(0)
  • 2020-12-13 04:44

    If you have subclassed a UITableViewCell, then you can customise the various elements of the cell by overriding the following:

    - (void)setHighlighted:(BOOL)highlighted animated:(BOOL)animated {
        if(highlighted) {
            self.backgroundColor = [UIColor redColor];
        } else {
            self.backgroundColor = [UIColor clearColor];
        }
    
        [super setHighlighted:highlighted animated:animated];
    }
    

    EDIT for iOS7: as Sasho stated, you also need

    cell.selectionStyle = UITableViewCellSelectionStyleNone
    
    0 讨论(0)
  • 2020-12-13 04:45

    Sublcass UITableViewCell and override setHighlighted:animated:

    You can define a custom selection color color by setting the backgroundColor (see WIllster's answer):

    - (void)setHighlighted:(BOOL)highlighted animated:(BOOL)animated {
        if(highlighted) {
            self.backgroundColor = [UIColor redColor];
        } else {
            self.backgroundColor = [UIColor clearColor];
        }
    
        [super setHighlighted:highlighted animated:animated];
    }
    

    You can define a custom background image by setting the backgroundView property:

    - (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];
    }
    
    0 讨论(0)
  • 2020-12-13 04:53

    The best way to set the selection is to set the selectedBackgroundView on the cell when you construct it.

    i.e.

    - (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
    }
    

    The image used should have a nice gradient (like the default selection). If you just want a flat color, you can use a UIView instead of a UIImageView and set the backgroundColor to the color you want.

    This background is then automatically applied when the row is selected.

    0 讨论(0)
  • 2020-12-13 04:53
    - (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];  
      }   
    }
    
    0 讨论(0)
提交回复
热议问题