iOS 8 UITableView separator inset 0 not working

前端 未结 30 1771
清酒与你
清酒与你 2020-11-22 14:38

I have an app where the UITableView\'s separator inset is set to custom values - Right 0, Left 0. This works perfectly in iOS 7.

30条回答
  •  耶瑟儿~
    2020-11-22 15:01

    This is the code that's working for me, in Swift:

    override func viewDidLoad() 
    {
        super.viewDidLoad()
        ...
        if tableView.respondsToSelector("setSeparatorInset:") {
            tableView.separatorInset = UIEdgeInsetsZero
        }
    }
    
    func tableView(tableView: UITableView, willDisplayCell cell: UITableViewCell,forRowAtIndexPath indexPath: NSIndexPath)
    {
        if cell.respondsToSelector("setSeparatorInset:") {
            cell.separatorInset.left = CGFloat(0.0)
        }
        if tableView.respondsToSelector("setLayoutMargins:") {
            tableView.layoutMargins = UIEdgeInsetsZero
        }
        if cell.respondsToSelector("setLayoutMargins:") {
            cell.layoutMargins.left = CGFloat(0.0)
        }
    }
    

    This seems the cleanest to me (for now), as all the cell/tableView edge/margin adjustments are done in the tableView:willDisplayCell:forRowAtIndexPath: method, without cramming unneccessary code into tableView:cellForRowAtIndexPath:.

    Btw, I'm only setting the cell's left separatorInset/layoutMargins, because in this case I don't want to screw up my constraints that I have set up in my cell.

    Code updated to Swift 2.2 :

     override func viewDidLoad() {
       super.viewDidLoad()       
    
        if tableView.respondsToSelector(Selector("setSeparatorInset:")) {
          tableView.separatorInset = UIEdgeInsetsZero
            }
        }
    
     override func tableView(tableView: UITableView, willDisplayCell cell: UITableViewCell,forRowAtIndexPath indexPath: NSIndexPath) {
            if cell.respondsToSelector(Selector("setSeparatorInset:")) {
                cell.separatorInset.left = CGFloat(0.0)
            }
            if tableView.respondsToSelector(Selector("setLayoutMargins:")) {
                tableView.layoutMargins = UIEdgeInsetsZero
            }
            if cell.respondsToSelector(Selector("setLayoutMargins:")) {
                cell.layoutMargins.left = CGFloat(0.0)
            }
        }
    

提交回复
热议问题