美文网首页iOS之报错上架填坑iOS 开发技巧程序员
UITableViewCell 处于选中状态,UIView 的背

UITableViewCell 处于选中状态,UIView 的背

作者: 要上班的斌哥 | 来源:发表于2017-06-17 15:50 被阅读814次

我想请问你这么一个问题?你在使用 UITableView 这个控件过程中,是不是会出现这么一种状况,当 UITableViewCell 处于选中状态的时候,UITableViewCell 里面的子控件背景颜色莫名其妙消失了 ?我就有遇到过这个问题,现在我来给你讲讲我的问题解决方法。如果你曾经或者现在遇到过这个问题并且也解决了,那么欢迎你在留言区分享你的解决方法,如果你还没有解决这个问题,那么欢迎你尝试我的问题解决方法。

Demo 时间

我们先上一个 Demo ,在 Main.storyboard 里面 的 ViewController 里面添加一个 TableView,并设置好 UITableViewDelegate 和UITableViewDataSource 。


TableView

再来新建一个 TableViewCell, 这个 Cell 叫做 DemoTableViewCell ,包含一个绿色背景的 Button,一个蓝色背景的 Label,一个红色背景的 View 。


image.png

接下来看看 ViewController 的代码

#import "ViewController.h"
#import "DemoTableViewCell.h"

@interface ViewController ()
@property (weak, nonatomic) IBOutlet UITableView *tableView;

@end

@implementation ViewController

- (void)viewDidLoad {
    [super viewDidLoad];
    // Do any additional setup after loading the view, typically from a nib.
    [self.tableView registerNib:[UINib nibWithNibName:@"DemoTableViewCell" bundle:nil] forCellReuseIdentifier:@"DemoTableViewCell"];
}

- (NSInteger)tableView:(UITableView *)tableView numberOfRowsInSection:(NSInteger)section{
    return 10;
}

- (UITableViewCell *)tableView:(UITableView *)tableView cellForRowAtIndexPath:(NSIndexPath *)indexPath{
   DemoTableViewCell *cell =  [tableView dequeueReusableCellWithIdentifier:@"DemoTableViewCell"];
    return cell;

}
@end

最后运行 Demo ,查看运行效果,效果正常。


运行效果

好了,我们选中 Demo 界面中的一行,看看会出现什么。UITableViewCell 处于选中状态,UIView 的背景颜色消失。

选中 Demo 界面中的一行

第一个解决方案,在 ViewController.m 文件,我们实现 UITableViewDelegate 的选择方法,并调用 UITableViewDelegate 的取消选择方法。

这个解决方案有一个缺陷不知道你发现没有 ? 从选中状态到取消选中状态之间的时间段内,UIView 的背景颜色消失的问题会出现,不过有时候这个方案就能够满足我们的需求了。

- (void)tableView:(UITableView *)tableView didSelectRowAtIndexPath:(NSIndexPath *)indexPath{
    [tableView deselectRowAtIndexPath:indexPath animated:NO];
}

接下来看看第二个解决方案 ,打开
DemoTableViewCell.m 文件,在 setSelected:animated: 方法里面重新设置 button,label,view 的背景颜色。

#import "DemoTableViewCell.h"

@implementation DemoTableViewCell

- (void)awakeFromNib {
    [super awakeFromNib];
    // Initialization code
}

- (void)setSelected:(BOOL)selected animated:(BOOL)animated {
    [super setSelected:selected animated:animated];
    self.button.backgroundColor = [UIColor greenColor];
    self.label.backgroundColor = [UIColor blueColor];
    self.view.backgroundColor = [UIColor redColor];
}
@end

运行 Demo 查看效果,你可以看到 UITableViewCell 处于选中状态,UIView 的背景颜色是正常的。

背景正常

当我们长按 UITableViewCell 的时候,UIView 的背景颜色消失还是会出现,那么我们就继续在 setHighlighted: animated: 方法里面重新设置 button,label,view 的背景颜色。

#import "DemoTableViewCell.h"

@implementation DemoTableViewCell

- (void)awakeFromNib {
    [super awakeFromNib];
    // Initialization code
}

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

- (void)setHighlighted:(BOOL)highlighted animated:(BOOL)animated{
    [super setHighlighted:highlighted animated:animated];
    [self restoreStatus];
}

-(void)restoreStatus{
    self.button.backgroundColor = [UIColor greenColor];
    self.label.backgroundColor = [UIColor blueColor];
    self.view.backgroundColor = [UIColor redColor];
}
@end

运行 Demo 查看效果。这次不管我们是选中 还是 长按 UITableViewCell 都不会出现 UIView 的背景颜色消失的问题,说明我们的第二个解决方案是可行的,能够完全处理掉这个问题。

image.png

第三个解决方案,不过我不建议使用这个方案。这个方案是将 UITableViewCell 的选择效果全部取消,也就意味着当用户点击或者长按 UITableViewCell 的时候都不会出现动画效果。这个解决方案不知道你能不能接受?反正我是接受不了。

#import "DemoTableViewCell.h"
@implementation DemoTableViewCell
- (void)awakeFromNib {
    [super awakeFromNib];
    // Initialization code
    self.selectionStyle = UITableViewCellSelectionStyleNone;
}
@end

总结

UITableViewCell 处于选中状态,UIView 的背景颜色消失,我猜测这个问题起源于当 UITableViewCell 处于选中状态也就是 selected ,UITableViewCell 的 View 也被置于 selected 。当 UITableViewCell 处于 highlighted,UITableViewCell 的 View 也被置于 highlighted。但是 UITableViewCell 的 setHighlighted: animated: 和 setSelected:animated: 并没有实现对 View 背景颜色的处理,所以出现了 view 背景颜色不对的问题。具体的分析可以参考 stackoverflow 的 这个帖子 https://stackoverflow.com/questions/5222736/uiview-backgroundcolor-disappears-when-uitableviewcell-is-selected

相关文章

网友评论

  • objcat:谢谢大神:blush:
  • 新月如火:谢谢分享
  • sun_dev:你好,我这边进入界面 需要默认选中一个cell ,我使用了下边的方法,但是并没有生效,请问是什么原因?
    - (UITableViewCell *)tableView:(UITableView *)tableView cellForRowAtIndexPath:(NSIndexPath *)indexPath {

    ...

    if(/* need to be selected*/){
    [cell setSelected:YES animated:NO];
    }

    return cell;
    }

    在cell的 setSelected:(BOOL)selected animated:(BOOL)animated 方法中设置了。
    - (void)setSelected:(BOOL)selected animated:(BOOL)animated {
    [super setSelected:selected animated:animated];

    self.selectedButton.selected = selected;
    }
  • 没有名字就是我的名字:欢迎各路大神一起来交流技术, 人数不多 超过100人改为付费群! 且行且珍惜号码:614194935
  • 花样棉花:很棒!
  • muchDrinkHotWat:如果subview内容很多很复杂呢?每一个都要重新设置背景色?那蛮有趣的
    要上班的斌哥:@muchDrinkHotWat 那就很酸爽了,最好另寻他法,不然要哭
  • LeeJay:还有一种方法:self.label.layer.backgroundColor
    要上班的斌哥:好的,我也去尝试下你的方案!
  • 妖妖零幺幺:我发现没有用啊 纯代码自定义cell
    要上班的斌哥:麻烦描述一下具体情况,或者贴一下代码,方便我帮你看看:blush:

本文标题:UITableViewCell 处于选中状态,UIView 的背

本文链接:https://www.haomeiwen.com/subject/mgzvqxtx.html