UITableView separator line disappears when selecting cells in iOS7

后端 未结 24 2152
耶瑟儿~
耶瑟儿~ 2020-12-05 04:01

In my tableView I set a separator line between cells. I am allowing selection of multiple cells. Here\'s my code for setting selected cell background color:

         


        
24条回答
  •  甜味超标
    2020-12-05 04:26

    Too exciting, I solved this problem. Add the following method call in a custom cell, and to set the color separator and frame. I'll hide the cell separator, and then customize the view on a load separator in superview. The impact separator cell is selected when this problem is solved friends

    @interface MyCustomTableViewCell(){
       UIView *customSeparatorView;
       CGFloat separatorHight;
    }
    @property (nonatomic,weak)UIView *originSeparatorView;
    @end
    
    -(void)setSeparatorWithInset:(UIEdgeInsets)insets{
    
    if (customSeparatorView) {
        customSeparatorView.frame = CGRectMake(insets.left, insets.top,self.width - insets.left - insets.right, self.originSeparatorView.height-insets.bottom - insets.top);
        self.originSeparatorView.hidden = YES;
        self.originSeparatorView.alpha = 0;
    }else{
        for (int i = ([self.contentView.superview.subviews count] - 1); i >= 0; i--) {
            UIView *subView = self.contentView.superview.subviews[i];
            if ([NSStringFromClass(subView.class) hasSuffix:@"SeparatorView"]) {
                self.originSeparatorView = subView;
                subView.hidden = YES;
                subView.alpha = 0;
                subView.frame = CGRectMake(insets.left, insets.top,self.width - insets.left - insets.right, subView.height-insets.bottom - insets.top);
    
                customSeparatorView = [[subView superview] viewWithTag:separatorViewTag];
                if (!customSeparatorView) {
                    customSeparatorView = [[UIView alloc] initWithFrame:subView.frame];
    
                    customSeparatorView.tag = separatorViewTag;
                    [[subView superview] addSubview:customSeparatorView];
                    customSeparatorView.backgroundColor = [subView backgroundColor];
                }
                [[subView superview] bringSubviewToFront:customSeparatorView];
                break;
            }
        }
      }
    }
    
    
    -(void)setSeparatorColorWithColor:(UIColor *)sepColor{
    if (customSeparatorView) {
        customSeparatorView.backgroundColor = sepColor;
    
        self.originSeparatorView.hidden = YES;
        self.originSeparatorView.alpha = 0;
    }else {
        for (int i = ([self.contentView.superview.subviews count] - 1); i >= 0; i--) {
            UIView *subView = self.contentView.superview.subviews[i];
            if ([NSStringFromClass(subView.class) hasSuffix:@"SeparatorView"]) {
               self.originSeparatorView = subView;
    
                if (sepColor) {
                    subView.hidden = YES;
                    subView.alpha = 0;
                    subView.backgroundColor = sepColor;
    
                    customSeparatorView = [[subView superview] viewWithTag:separatorViewTag];
                    if (!customSeparatorView) {
                        customSeparatorView = [[UIView alloc] initWithFrame:subView.frame];
    
                        customSeparatorView.tag = separatorViewTag;
                        [[subView superview] addSubview:customSeparatorView];
                        customSeparatorView.backgroundColor = [subView backgroundColor];
                    }
                    [[subView superview] bringSubviewToFront:customSeparatorView];
                }
                break;
            }
        }
      }
    }
    
     -(void)layoutSubviews{
        [super layoutSubviews];
        [self setSeparatorWithInset:UIEdgeInsetsMake(0, 0, 0, 0)];
        [self setSeparatorColorWithColor:[UIColor colorWithRed:31/255.0 green:32/255.0f blue:35/255.0 alpha:0.2]];
     }
    

提交回复
热议问题