swift開發UITableViewCell的分割線與屏幕邊緣對齊顯示的解決方案swift
iOS開發中咱們使用UITableView控件時會發現每一個UITableviewCell的分割線左側距離屏幕有必定的間距,這樣的默認效果也很好,可是每每咱們會遇到讓把cell的分割線與屏幕的左右邊緣對齊就是讓分割線的寬度和屏幕的寬度相同的顯示效果。spa
介紹兩種實現的方法:代理
第一種方法:blog
能夠經過在cell中自定義一個cell底部subView的方法,使得subView的左右約束與屏幕的左右間距爲0來實現,此種方法就不作重點說明。開發
第二種方法:io
經過實現UITableViewDelegate中的代理方法來實現table
optional public func tableView(_ tableView: UITableView, willDisplay cell: UITableViewCell, forRowAt indexPath: IndexPath)
具體實現以下代碼:class
//分割線從左端頂部顯示(使cell的)分割線與屏幕的左右兩端對齊顯示 func tableView(_ tableView: UITableView, willDisplay cell: UITableViewCell, forRowAt indexPath: IndexPath) { if(cell.responds(to: #selector(setter: UITableViewCell.separatorInset))){ cell.separatorInset = .zero } if(cell.responds(to: #selector(setter: UITableViewCell.layoutMargins))){ cell.layoutMargins = .zero } }