Grouped UITableview remove outer separator line

前端 未结 19 2181
遇见更好的自我
遇见更好的自我 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:48

    Here's a Swift solution that works on iOS 8 and 9.

    Define a protocol with a default implementation:

    protocol CellSeparatorRemovable { }
    
    extension CellSeparatorRemovable {
        func removeSeparatorLinesFromCell(cell: UITableViewCell, section: Int, row: Int, indexPath: NSIndexPath) {
            guard (section, row) == (indexPath.section, indexPath.row) else { return }
    
            for view in cell.subviews where view != cell.contentView {
                view.removeFromSuperview()
            }
        }
    }
    

    Then, wherever you want to use it, conform to the CellSeparatorRemovable protocol and call its method from …willDisplayCell…:

    class SomeVC: UITableViewController, CellSeparatorRemovable {
        override func tableView(tableView: UITableView, willDisplayCell cell: UITableViewCell, forRowAtIndexPath indexPath: NSIndexPath) {
            removeSeparatorLinesFromCell(cell, section: 1, row: 1, indexPath: indexPath)
        }
    }
    

    This is a minimal solution; you may need to refactor it if you're dealing with many cells to avoid excessive recursion and/or cell reuse issues.

提交回复
热议问题