ios7 UITableView 分割线在 使用selectedBackgroundView 选中时有些不显示

转自:TB&Boy UITableView 分割线在 使用selectedBackgroundView 选中时有些不显示

UITableView  选中cell ,默认会有一个灰色的背景遮罩效果,这个灰色遮罩就是cell 自带的

selectedBackgroundView

我们可以设置selectedBackgroundView 的frame  、和 背景颜色

selectedBackgroundView.backgroundColor

 1 - (UITableViewCell *)tableView:(UITableView *)tableView cellForRowAtIndexPath:(NSIndexPath *)indexPath {
 2 
 3        cell.selectedBackgroundView.backgroundColor = [UIColor redColor];
 4        cell.selectedBackgroundView.frame = cell.frame;
 5 
 6 }
 7 
 8 -(void)tableView:(UITableView *)tableView didSelectRowAtIndexPath:(NSIndexPath *)indexPath{
 9        [self performSelector:@selector(unselectCell:) withObject:nil afterDelay:0.2];
10 
11 }
12 
13 -(void)unselectCell:(id)sender{
14     
15     [_setUpTableView deselectRowAtIndexPath:[_setUpTableView indexPathForSelectedRow] animated:YES];
16 }

这样就可以自定义选中效果啦。问题来拉,当选中一个cell时,你会发现这个cell 想临的cell 的分割线没啦(按下cell ,没有弹起,遮罩显示时)。

这™明显bug ,在ios7之后。

http://stackoverflow.com/questions/19212476/uitableview-separator-line-disappears-when-selecting-cells-in-ios7

这个里面的回答都试过遍了还是不行。

自己搞吧,决定不用系统的分割线,自己加一个试试看。

在你cell 的基类中或 自定义cell 中添加这两个方法

 1 #define  separatorViewTag  10456
 2 
 3 @interface MyCustomTableViewCell(){
 4    UIView *customSeparatorView;
 5    CGFloat separatorHight;
 6 }
 7 @property (nonatomic,weak)UIView *originSeparatorView;
 8 @end
 9 
10 
11 /**
12  *  设置分割线 , separatorInset 不一定能实现
13  *  解决选中cell selectedBackgroundView 显示时 分割线显示不全
14  *  问题参见:http://stackoverflow.com/questions/19212476/uitableview-separator-line-disappears-when-selecting-cells-in-ios7
15  *  @param insets insets description
16  */
17 -(void)setSeparatorWithInset:(UIEdgeInsets)insets{
18 
19 if (customSeparatorView) {
20     customSeparatorView.frame = CGRectMake(insets.left, insets.top,self.width - insets.left - insets.right, self.originSeparatorView.height-insets.bottom - insets.top);
21     self.originSeparatorView.hidden = YES;
22     self.originSeparatorView.alpha = 0;
23 }else{
24     for (int i = ([self.contentView.superview.subviews count] - 1); i >= 0; i--) {
25         UIView *subView = self.contentView.superview.subviews[i];
26         if ([NSStringFromClass(subView.class) hasSuffix:@"SeparatorView"]) {
27             self.originSeparatorView = subView;
28             subView.hidden = YES;
29             subView.alpha = 0;
30             subView.frame = CGRectMake(insets.left, insets.top,self.width - insets.left - insets.right, subView.height-insets.bottom - insets.top);
31 
32             customSeparatorView = [[subView superview] viewWithTag:separatorViewTag];
33             if (!customSeparatorView) {
34                 customSeparatorView = [[UIView alloc] initWithFrame:subView.frame];
35 
36                 customSeparatorView.tag = separatorViewTag;
37                 [[subView superview] addSubview:customSeparatorView];
38                 customSeparatorView.backgroundColor = [subView backgroundColor];
39             }
40             [[subView superview] bringSubviewToFront:customSeparatorView];
41             break;
42         }
43     }
44   }
45 }
46 
47 
48 -(void)setSeparatorColorWithColor:(UIColor *)sepColor{
49 if (customSeparatorView) {
50     customSeparatorView.backgroundColor = sepColor;
51 
52     self.originSeparatorView.hidden = YES;
53     self.originSeparatorView.alpha = 0;
54 }else {
55     for (int i = ([self.contentView.superview.subviews count] - 1); i >= 0; i--) {
56         UIView *subView = self.contentView.superview.subviews[i];
57         if ([NSStringFromClass(subView.class) hasSuffix:@"SeparatorView"]) {
58            self.originSeparatorView = subView;
59 
60             if (sepColor) {
61                 subView.hidden = YES;
62                 subView.alpha = 0;
63                 subView.backgroundColor = sepColor;
64 
65                 customSeparatorView = [[subView superview] viewWithTag:separatorViewTag];
66                 if (!customSeparatorView) {
67                     customSeparatorView = [[UIView alloc] initWithFrame:subView.frame];
68 
69                     customSeparatorView.tag = separatorViewTag;
70                     [[subView superview] addSubview:customSeparatorView];
71                     customSeparatorView.backgroundColor = [subView backgroundColor];
72                 }
73                 [[subView superview] bringSubviewToFront:customSeparatorView];
74             }
75             break;
76         }
77     }
78   }
79 }
80 
81  -(void)layoutSubviews{
82     [super layoutSubviews];
83     [self setSeparatorWithInset:UIEdgeInsetsMake(0, 0, 0, 0)];
84     [self setSeparatorColorWithColor:[UIColor colorWithRed:31/255.0 green:32/255.0f blue:35/255.0 alpha:0.2]];
85  }

一个是设置分割线frame  的 一个是设置颜色。

其中遍历cell的subview  倒序。(提高效率)找到分割线,隐藏掉,重写一个view 加在分割线的superview 上。这样上面问题中使用selectedBackgroundView选中cell 时影响分割线的问题就解决啦。

注:tableview  使用section展示好像没事(把cell 都放到section 里),我项目设置页面就是这样没有问题使用selectedBackgroundView。当只有一个section时会出现上述问题。

原文地址:https://www.cnblogs.com/qq744890760/p/5127391.html