Grouped UITableview remove outer separator line

前端 未结 19 2175
遇见更好的自我
遇见更好的自我 2020-12-02 10:58

I have a grouped UITableview which is created programatically. Also I have a cell with xib file populated in tableview programmatically as well. So far so good. But I want t

19条回答
  •  野趣味
    野趣味 (楼主)
    2020-12-02 11:24

    iOS 13, Swift 5

    In the custom cell class:

    override func layoutSubviews(){
        super.layoutSubviews()
    
        for subview in subviews where (subview != contentView && abs(subview.frame.width - frame.width) <= 0.1 && subview.frame.height < 2) {
            subview.removeFromSuperview()                           //option #1 -- remove the line completely
            //subview.frame = subview.frame.insetBy(dx: 16, dy: 0)  //option #2 -- modify the length
        }
    }
    

    I want to thank @cook for this solution, as I built on top of it. I had some issues with their solution:

    1. it was removing the default highlight/selected background view, so I added an extra check on the subview's height.
    2. I put my code in layoutSubviews() and haven't had a single issue.
    3. I implemented an approximation between two CGFloats instead of using the equality operator ==, which sounds error-prone to me.

    I added the "option #2" in the code, as that's the solution I was personally looking for (I wanted to maintain the separator, but I wanted it to be at the same indentation level as the regular cell separators, in my case a value of 16).

提交回复
热议问题