UITableViewのセパレーターの左側にある余白を消す方法

UITableViewのセパレーターの左側にある余白を消す方法

2018年10月13日
スポンサーリンク

今回はUITableViewのセパレーター(下線)の左側にある余白を消す方法をご紹介します。

全てのセルの余白を消す場合

全てのセルの余白を消す場合は、UITableViewseparatorInsetの値をUIEdgeInsets.zeroに設定します。

// セパレーターの左側の余白を消す
tableView.separatorInset = UIEdgeInsets.zero

 

特定のセルの余白のみを消す場合

特定のセルの余白のみを消す場合は、セルの内容を返すDataSourceメソッド内で、対象のUITableViewCellseparatorInsetの値のみをUIEdgeInsets.zeroに設定します。

let cellTexts = ["りんご", "ゴリラ", "ラジオ", "オレンジ", "ジュース", "スイカ"]

func tableView(_ tableView: UITableView, cellForRowAt indexPath: IndexPath) -> UITableViewCell {
    // セルを生成
    let cell = UITableViewCell(style: .default, reuseIdentifier: nil)
        
    // セルのテキストを設定
    cell.textLabel?.text = cellTexts[indexPath.row]
        
    // テキストが ラジオ の場合、セパレーターの余白を消す
    if cellTexts[indexPath.row] == "ラジオ" {
        cell.separatorInset = UIEdgeInsets.zero
    }
        
    return cell
}