SwiftでUITableViewを使うことはよくありますよね。UITableViewCellをリスト表示したときに、罫線がぴったり表示されないことってありませんか?言葉で書いてもわかりづらいと思うので、キャプチャでお見せするとこういう現象です。
Contents
キャプチャ
リストの罫線の左側に謎のスペースが…!クリティカルな実害はありませんがかなり気持ち悪いですね。そういうときは、UITableViewDataSource内に下記メソッドを追加しましょう。
追加するメソッド
1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 |
func tableView(tableView: UITableView, willDisplayCell cell: UITableViewCell, forRowAtIndexPath indexPath: NSIndexPath) { // Remove seperator inset if cell.respondsToSelector(Selector("setSeparatorInset:")) { cell.separatorInset = UIEdgeInsetsZero } // Prevent the cell from inheriting the Table View's margin settings if cell.respondsToSelector(Selector("setPreservesSuperviewLayoutMargins:")) { cell.preservesSuperviewLayoutMargins = false } // Explictly set your cell's layout margins if cell.respondsToSelector(Selector("setLayoutMargins:")) { cell.layoutMargins = UIEdgeInsetsZero } } |
こういう風に改善されます
地味に便利です。
1件のコメント