Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

In iOS7, cell.selectedBackgroundView is not showing

Working on converting an iOS6 app to iOS7, and they are using this to setup a grouped tableview style.

    cell.backgroundView = aView;
    cell.selectedBackgroundView = bView;

When the app loads, it loads the backgroundView correctly, but when I click on the cell the selectedBackgroundView no longer works in iOS7. The clicking selects the cell so that is working, but the selectBackgroundView just doesn't show.

Any suggestions? The only thing I can think is not use selectedBackgroundView and just add and remove subviews to the cell each time they are selected and unselected.

/// UPDATE ///

Put this in my cellForRowAtIndex and still does NOT work.

UIView *bgColorView = [[UIView alloc] initWithFrame:CGRectMake(0, 0, 100, 100)];
    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;

//// UPDATE 2 ///////////

If I put this in my cellforRowAtIndex

cell.selectedBackgroundView = nil;
    cell.backgroundView = nil;
    UIView *bgColorView = [[UIView alloc] initWithFrame:CGRectMake(0, 0, 100, 100)];
    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;

    UIView *bgSColorView = [[UIView alloc] initWithFrame:CGRectMake(0, 0, 100, 100)];
    bgSColorView.backgroundColor = [UIColor colorWithRed:(106.0/255.0) green:(201.0/255.0) blue:(255.0/255.0) alpha:1.0]; // perfect color suggested by @mohamadHafez
    bgSColorView.layer.masksToBounds = YES;
    cell.backgroundView = bgSColorView;

Both the show cell.background as bgSColorView, but when I click the cell nothing happens (aka the selectedBackgroundView is NOT shown? Why would the selectedBackgroundView not be promoted and shown?

/// UPDATE 3 //// I put this in my setSelectin:animation: for my cell class (removed the stuff above).

 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;
self.selectedBackgroundView = bgColorView;

UIView *bgSColorView = [[UIView alloc] init];
bgSColorView.backgroundColor = [UIColor colorWithRed:(106.0/255.0) green:(201.0/255.0) blue:(25.0/255.0) alpha:1.0]; // perfect color suggested by @mohamadHafez
bgSColorView.layer.masksToBounds = YES;
self.backgroundView = bgSColorView;



[super setSelected:selected animated:animated];

Now it appears correct, as I want it. But the second I click any cell BOOOM the background and selectionBackgroundView are removed instantly, vanished.

like image 577
jdog Avatar asked Dec 02 '22 16:12

jdog


2 Answers

I had this issue recently.

Found my problem was in my .xib I had the selectionStyle set to UITableViewCellSelectionStyleNone.

Once I set self.selectionStyle = UITableViewCellSelectionStyleDefault; my selectedBackgroundView started showing correctly.

So I did this.

- (void)awakeFromNib
{
    [super awakeFromNib];

    self.selectionStyle = UITableViewCellSelectionStyleDefault;

    UIView *selectedBackgroundView = [[UIView alloc] init];
    selectedBackgroundView.backgroundColor = [UIColor redColor];
    self.selectedBackgroundView = selectedBackgroundView;
}
like image 138
Jason Jarrett Avatar answered Dec 13 '22 11:12

Jason Jarrett


Whenever I can't get it to work in tableView:cellForRowAtIndexPath: I just subclass UITableViewCell and override the setSelected:animated method like so:

- (void)setSelected:(BOOL)selected animated:(BOOL)animated
{
    [super setSelected:selected animated:animated];

    // Configure the view for the selected state
    UIImage *image = [UIImage imageNamed:@"selected_cell.png"];
    self.selectedBackgroundView = [[UIImageView alloc]initWithImage:image];
}

I just tried this in a different project and it didn't work .. Haha. Here's what I use now:

 - (void)setSelected:(BOOL)selected animated:(BOOL)animated
        {
             [super setSelected:selected animated:animated];

            if (selected) {
                contentView.backgroundColor = SharedColors.Blue.ToUIColor ();
            } else {
                contentView.backgroundColor = SharedColors.Black.ToUIColor ();
            }
        }
like image 25
ebandersen Avatar answered Dec 13 '22 13:12

ebandersen