UITableView separator line disappears when selecting cells in iOS7

后端 未结 24 2053
耶瑟儿~
耶瑟儿~ 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:19

    Past it in your UITableViewCell class.

    override func layoutSubviews() {
        super.layoutSubviews()
    
        subviews.forEach { (view) in
            if type(of: view).description() == "_UITableViewCellSeparatorView" {
                view.alpha = 1.0
            }
        }
    }
    
    0 讨论(0)
  • 2020-12-05 04:20

    I haven't gotten to the bottom of it yet (at first glance it seems like an iOS 7 bug..), but I have found a workaround. In tableView:didSelectRowAtIndexPath, if you send both messages below, the issue is visually resolved (with the probable performance cost).

    [tableView deselectRowAtIndexPath:indexPath animated:YES];
    [tableView reloadRowsAtIndexPaths:@[indexPath] withRowAnimation:UITableViewRowAnimationAutomatic];
    

    For this to work (for me), deselectRowAtIndexPath:animated: must contain animated:YES. The animation used for reloadRowsAtIndexPaths:withRowAnimation: doesn't matter.

    0 讨论(0)
  • 2020-12-05 04:20

    This still seems to be a problem as of iOS 7.0.3, but I've worked around it with an unsophisticated means of faking the separator.

    By first setting the UITableView's separator style to UITableViewCellSeparatorStyleNone. You can then use a custom UITableViewCell subclass to fake the separator between cells for both selected and unselected states:

    @implementation MyTableViewCellSubclass
    
    - (id)initWithStyle:(UITableViewCellStyle)style reuseIdentifier:(NSString *)reuseIdentifier
    {
        self = [super initWithStyle:style reuseIdentifier:reuseIdentifier];
        if (self) {
    
            CGRect frame = self.bounds;
            frame.origin.y = frame.size.height - 1.f;
            frame.size.height = 1.f;
    
            // Selected background view
            //
            UIView * separatorView = [[UIView alloc] initWithFrame:frame];
            separatorView.backgroundColor = [UIColor darkGrayColor];
            separatorView.autoresizingMask = UIViewAutoresizingFlexibleWidth|UIViewAutoresizingFlexibleTopMargin;
    
            UIView * selectedView = [[UIView alloc] initWithFrame:self.bounds];
            selectedView.backgroundColor = [UIColor lightGrayColor];
            [selectedView addSubview:separatorView];
    
            self.selectedBackgroundView = selectedView;
    
            // Add separator view to content view for unselected state
            //
            UIView * separatorView2 = [[UIView alloc] initWithFrame:frame];
            separatorView2.backgroundColor = [UIColor darkGrayColor];
            separatorView2.autoresizingMask = UIViewAutoresizingFlexibleWidth|UIViewAutoresizingFlexibleTopMargin;
    
            [self.contentView addSubview:separatorView2];
    
    
        }
        return self;
    }
    
    
    @end
    
    0 讨论(0)
  • 2020-12-05 04:20

    - cellForRowAtIndexPath

    Create two separator views (sv1, sv2)
    
    [cell addsubview:sv1];
    [cell.selectedBackgroundView addsubview:sv2];
    

    - didSelectRowAtIndexPath

    [tableView deselectRowAtIndexPath:indexPath animated:NO];
    
    0 讨论(0)
  • 2020-12-05 04:21

    use this:

    - (BOOL)tableView:(UITableView *)tableView shouldHighlightRowAtIndexPath:(NSIndexPath *)indexPath
    {
        UITableViewCell *cell = (UITableViewCell *)[tableView cellForRowAtIndexPath:indexPath];
        UIView *selectionColor = [[UIView alloc] init];
        selectionColor.backgroundColor = [UIColor clearColor];
        cell.selectedBackgroundView = selectionColor;
        //71
        UIView* separatorLineView = [[UIView alloc] initWithFrame:CGRectMake(0, 71, 320, 2)];/// change size as you need, where - 71 - y coordinate, 320 - weight, 2 - height
      //  separatorLineView.backgroundColor = [UIColor colorWithPatternImage:[UIImage imageNamed:@"divider_goriz.png"]];// you can also put image here
        separatorLineView.backgroundColor = [UIColor redColor];
        [cell.selectedBackgroundView addSubview:separatorLineView];
        return YES;
    }
    
    0 讨论(0)
  • 2020-12-05 04:23

    what solved the issue for me was reloading the data after beginUpdates and endUpdates:

        private func animateCellHeighChangeForTableView(tableView: UITableView, withDuration duration: Double) {
            UIView.animateWithDuration(duration) { () -> Void in
               tableView.beginUpdates();
               tableView.endUpdates();
               tableView.reloadData();
            }
        }
    
    0 讨论(0)
提交回复
热议问题