ios7 UITableViewCell selectionStyle won't go back to blue

后端 未结 4 1925
轻奢々
轻奢々 2021-01-30 21:32

Xcode 5.0, iOS 7 and updating an existing app. UITableView selected row is now gray, not blue.

From what I\'ve read they changed the default selection

相关标签:
4条回答
  • 2021-01-30 21:44

    I know I'm really late to the party, but I'll offer my IOS10 work around as well. Don't touch any of your other code, but add:

    -(void)tableView:(UITableView *)tableView didSelectRowAtIndexPath:(NSIndexPath *)indexPath
    {
        UITableViewCell *cell = [tableView cellForRowAtIndexPath:indexPath];
        cell.backgroundColor = [UIColor blueColor];
        cell.textLabel.textColor = [UIColor whiteColor];
    
         ... whatever else you do ...
    }
    
    
    -(void)tableView:(UITableView *)tableView didDeselectRowAtIndexPath:(nonnull NSIndexPath *)indexPath
    {
        UITableViewCell *cell = [tableView cellForRowAtIndexPath:indexPath];
        cell.backgroundColor = [UIColor whiteColor];
        cell.textLabel.textColor = [UIColor blackColor];
    }
    
    0 讨论(0)
  • 2021-01-30 21:53

    Probably it could help you. I have my custom cell and to make it selected with needed color I have overwrite setHighlighted and setSelected now it's look like that

    #define IOS_7 (floor(NSFoundationVersionNumber) > NSFoundationVersionNumber_iOS_6_1 ? YES : NO)
    
    
        - (void)setSelected:(BOOL)selected animated:(BOOL)animated
    {
        [super setSelected:selected animated:animated];
        [self changeSelectionColorForSelectedORHiglightedState:selected];
        // Configure the view for the selected state
    }
    
    - (void)setHighlighted:(BOOL)highlighted animated:(BOOL)animated
    {
        [super setHighlighted:highlighted animated:animated];
        [self changeSelectionColorForSelectedORHiglightedState:highlighted];
    }
    
    - (void)changeSelectionColorForSelectedORHiglightedState:(BOOL)state
    {
        if (IOS_7) {
            if (state) {
                self.contentView.backgroundColor = [UIColor blueColor];
            }
        }
    }
    
    0 讨论(0)
  • 2021-01-30 21:55

    There is only one selectionStyle in iOS7, to change you need to do this manually like below:

    - (UITableViewCell *)tableView:(UITableView *)tableView cellForRowAtIndexPath:(NSIndexPath *)indexPath
    { 
        ....
        UIView *bgColorView = [[UIView alloc] init];
        bgColorView.backgroundColor = [UIColor colorWithRed:(76.0/255.0) green:(161.0/255.0) blue:(255.0/255.0) alpha:1.0]; // perfect color suggested by @mohamadHafez
        bgColorView.layer.masksToBounds = YES;
        cell.selectedBackgroundView = bgColorView;
        ....
        return cell;
    }
    
    0 讨论(0)
  • 2021-01-30 21:55

    I know this has already been answered but the last thing I wanted to do was touch all of my cellForRowAtIndexPath methods. So, I used an appearance proxy in my App Delegate. I took @null's code above to set the selected background view and in the applicationDidFinishLaunching:withOptions: method I placed this code.

    UIView *bgColorView = [[UIView alloc] init];
    //the rest of null's code to make the view
    [[UITableViewCell appearance] setSelectedBackgroundView:bgColorView];
    

    Then to make the white text hi light:

    [[UILabel appearanceWhenContainedIn:[UITableViewCell class], nil] setHighlightedTextColor:[UIColor whiteColor]];
    

    This made a global change in my app. The appearance proxy was introduced in iOS5 and Mattt has a great article on how to use it at his NSHipster blog.

    0 讨论(0)
提交回复
热议问题