ios - 在 iOS7 中,cell.selectedBackgroundView 没有显示

标签 ios objective-c uitableview

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

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

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

有什么建议吗?我唯一能想到的就是不使用 selectedBackgroundView,而只是在每次选择和取消选择时向单元格添加和删除 subview 。

///更新///

将它放入我的 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 立即被删除,消失了。

最佳答案

我最近遇到了这个问题。

发现我的问题出在我的 .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;
}

关于ios - 在 iOS7 中,cell.selectedBackgroundView 没有显示,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/21918448/

相关文章:

iOS 8 UITableView 第一行高度错误

ios - 未在 UITableView 中调用 scrollviewDidEndDragging

objective-c - [objrelease]之后引用计数仍然是1,此时应该释放它

objective-c - ivar 不会响应 NSMutableString 方法

ios - 在编辑模式下重新排序时更改 UITableViewCell 的背景颜色

iphone - 在 UITableView reloadData 之后在 UITextView 中设置选择?

ios - 在 iOS 上无法滚动 X

ios - ImageView 不会变成圆形

ios - 室内图集 : iOS SDK doesn't give accurate position when device stops moving

ios - UITableView: anchor 节标题