ios7适配--uitableviewcell选中效果

ios7 UITableViewCell selectionStyle won't go back to blue

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 selectionStyle to gray. But "Blue" is still an option in IB and UITableViewCellSelectionStyleBlue still exists. Checking out the new HIG, it doesn't look like they removed the blue and the "Settings" app still using blue cell selection.

I've tried setting the value in IB and in code, but no luck. Any ideas on what I need to do to get the blue selection style back?

share|edit|flag
 
   
   
I am unable to check it out right now, but in the cellForRowAtIndexPath method, it does not work to use the statement, cell.selectionStyle = UITableViewCellSelectionStyleBlue;? – for i in range awesomeSep 13 at 20:50
   
   
Just tested it out- it does not work. I will try to figure it out and get back to you if I can fix it – for i in range awesome Sep 13 at 21:12
   
add comment

2 Answers

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 blueColor];
    bgColorView.layer.masksToBounds = YES;
    cell.selectedBackgroundView = bgColorView;
    ....
    return cell;
}
share|edit|flag
 
   
   
I was thinking about trying something like this- only problem is that blueColor is the ugliest possible shad of blue for this. If someone wants to actually use this in an app, then it would require using RGB combos for a better color – for i in range awesome Sep 14 at 16:38
   
   
So what I'm hearing is, blue as a default either no longer exists or isn't functioning properly. That was pretty much my conclusion too, but it seems awfully strange they wouldn't take it out of the UI or deprecate it if wasn't going to have a future. – DBD Sep 16 at 12:31
   
   
While not overly happy with this answer, this type of approach does seem to be the only which currently works (hopefully that will change soon). So I'm marking as the accepted answer. – DBD Sep 19 at 12:52
   
   
I agree, but what to do? it is not the best solution but apparently it is the only way to do the job, Apple has taken some actions in iOS7 made a lot of developers unhappy, for example you cannot add subview in UIActionSheet any more! this is really sad especially there is no workaround like your case and you should use another custom component rather than ActionSheet. – null Sep 19 at 15:24
   
add comment

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];
        }
    }
}
share|edit|flag
  add comment
原文地址:https://www.cnblogs.com/sunshine-anycall/p/3342246.html