UITableView separator line disappears when selecting cells in iOS7

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

    I needed the following:

    "When user selects row, selection background color is transparent/white/whatever you may call it and separator lines don't disappear"

    I've looked as well for a solution for the following problem:

    "When I select a row in a table (plain type table) I had selection colour grey, and if I set cell.selectionStyle to none -> Separators between cells disappeared."

    Xcode - 9.2 version

    Found the following solution:

    1. in 'tableView (....cellForRowAT...)' let colorView = UIView(frame: CGRect(x: 0.0, y: 3.0, width: cell.frame.width, height: cell.frame.height - 1.0)) colorView.backgroundColor = UIColor.white UITableViewCellClass.appearance().selectedBackgroundView = colorView

    UITableViewCellClass - is your prototype cell class it makes possible to change selection color to white

    1. in 'tableView (...didSelectRowAt)' cell.selectionStyle = .none

    2. in UITableViewCellClass (your prototype cell class)

      override func layoutSubviews() { super.layoutSubviews()

      subviews.forEach { (view) in
          if type(of: view).description() == "_UITableViewCellSeparatorView" {
              view.alpha = 1.0
          }
      }
      

      }

    it allows to keep selected row with check mark and all separators are in place.

提交回复
热议问题