在iOS7中,未显示cell.selectedBackgroundView

jdo*_*dog 3 objective-c uitableview ios

致力于将iOS6应用程序转换为iOS7,他们正在使用它来设置分组的tableview样式.

    cell.backgroundView = aView;
    cell.selectedBackgroundView = bView;
Run Code Online (Sandbox Code Playgroud)

当应用程序加载时,它会正确加载backgroundView,但是当我单击该单元格时,selectedBackgroundView在iOS7中不再有效.单击选择单元格以使其正常工作,但selectBackgroundView不会显示.

有什么建议?我唯一能想到的就是不使用selectedBackgroundView,只需在每次选中和取消选中子视图时添加和删除子视图.

///更新///

把这个在我的cellForRowAtIndex和仍然工作.

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;
Run Code Online (Sandbox Code Playgroud)

////更新2 ///////////

如果我把它放在我的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;
Run Code Online (Sandbox Code Playgroud)

将cell.background显示为bgSColorView,但是当我单击单元格时没有任何反应(也就是未显示selectedBackgroundView?为什么selectedBackgroundView不会被提升和显示?

///更新3 ////我把它放在我的setSelectin:animation中:用于我的手机类(删除上面的内容).

 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];
Run Code Online (Sandbox Code Playgroud)

现在它似乎正确,因为我想要它.但第二次我点击任何单元格BOOOM背景和selectionBackgroundView被立即删除,消失了.

Jas*_*ett 15

我最近遇到过这个问题.

发现我的问题是在我.xibselectionStyle设置为UITableViewCellSelectionStyleNone.

一旦我设置self.selectionStyle = UITableViewCellSelectionStyleDefault;我的selectedBackgroundView开始正确显示.

所以我这样做了.

- (void)awakeFromNib
{
    [super awakeFromNib];

    self.selectionStyle = UITableViewCellSelectionStyleDefault;

    UIView *selectedBackgroundView = [[UIView alloc] init];
    selectedBackgroundView.backgroundColor = [UIColor redColor];
    self.selectedBackgroundView = selectedBackgroundView;
}
Run Code Online (Sandbox Code Playgroud)