特定の UITableViewCell だけ separator の線消したい

tableView で この cell だけ separator 消したいよ〜 という時の方法。

ググると、全ての tableView の separator 消して追加したいやつだけ追加するコード書こうとか、全部の tableView の線を消す方法とか出るけど、特定の cell だけ separator 消す というのをしたい。

やり方

import UIKit

class HogeTableViewCell: UITableViewCell {
    static let height: CGFloat = 88

    override init(style: UITableViewCellStyle, reuseIdentifier: String?) {
        super.init(style: style, reuseIdentifier: reuseIdentifier)
    }

    convenience init(_ separatorInsetLeft: CGFloat) {
        self.init(style: .default, reuseIdentifier: nil)
        separatorInset = UIEdgeInsets(top: 0, left: separatorInsetLeft, bottom: 0, right: 0)
    }

    required init?(coder aDecoder: NSCoder) {
        fatalError("init(coder:) has not been implemented")
    }
}

separatorInset の left を画面右端にすると、線が見えなくなる。

使い方

    func tableView(_ tableView: UITableView, cellForRowAt indexPath: IndexPath) -> UITableViewCell {
        return FooterTableViewCell(view.bounds.width)
    }

これでいける、register nib している場合は init じゃダメだと思うので cell.separatorInset = ... で書けばいけると思う。