在iOS7中,未显示cell.selectedBackgroundView

时间:2014-02-20 20:10:08

标签: ios objective-c uitableview

致力于将iOS6应用转换为iOS7,他们正在使用它来设置分组的桌面视图样式。

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

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

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

///更新///

将它放在我的cellForRowAtIndex中,仍然 NOT 工作。

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;

////更新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;

将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];

现在它看起来是正确的,因为我想要它。但是第二次我点击任何单元格BOOOM背景和selectionBackgroundView被立即删除,消失了。

3 个答案:

答案 0 :(得分:15)

我最近遇到过这个问题。

发现我的问题出在.xib我将selectionStyle设置为UITableViewCellSelectionStyleNone。

设置self.selectionStyle = UITableViewCellSelectionStyleDefault;后,我的selectedBackgroundView开始正确显示。

所以我这样做了。

- (void)awakeFromNib
{
    [super awakeFromNib];

    self.selectionStyle = UITableViewCellSelectionStyleDefault;

    UIView *selectedBackgroundView = [[UIView alloc] init];
    selectedBackgroundView.backgroundColor = [UIColor redColor];
    self.selectedBackgroundView = selectedBackgroundView;
}

答案 1 :(得分:2)

每当我无法在tableView中运行它:cellForRowAtIndexPath:我只是继承UITableViewCell并覆盖setSelected:animated方法,如下所示:

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

我只是在一个不同的项目中试过这个并且它没有工作......哈哈。这是我现在使用的:

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

答案 2 :(得分:0)

override func setSelected(selected: Bool, animated: Bool) {
    super.setSelected(selected, animated: animated)
    var view = UIView(frame: self.bounds)
    view.backgroundColor = UIColor.redColor()
    self.selectedBackgroundView = view


}