LoginSignup
47

More than 5 years have passed since last update.

UITableViewでcellとcellの間を空ける方法

Posted at

1つのセクションに1セルとして、空白なヘッダービューとフッタービューでスペーシングすればいい

TableViewDelegateImpl.swift
class TableViewDelegateImpl : NSObject, UITableViewDelegate {
    func tableView(tableView: UITableView, heightForRowAtIndexPath indexPath: NSIndexPath) -> CGFloat {
        return 44 // 適当なセルの高さ
    }
    func tableView(tableView: UITableView, heightForHeaderInSection section: Int) -> CGFloat {
        return 5 // セルの上部のスペース
    }
    func tableView(tableView: UITableView, heightForFooterInSection section: Int) -> CGFloat {
        return 5 // セルの下部のスペース
    }
    func tableView(tableView: UITableView, willDisplayHeaderView view: UIView, forSection section: Int) {
        view.tintColor = UIColor.clearColor() // 透明にすることでスペースとする
    }
    func tableView(tableView: UITableView, willDisplayFooterView view: UIView, forSection section: Int) {
        view.tintColor = UIColor.clearColor() // 透明にすることでスペースとする
    }
}
TableViewDataSourceImpl.swift
class TableViewDataSourceImpl : NSObject, UITableViewDataSource {
    func tableView(tableView: UITableView, numberOfRowsInSection section: Int) -> Int {
        return 1 // 普通はここで表示したいセルの数を返すが、セクションのヘッダーとフッターでスペーシングしているので、固定で1を返す
    }
    func numberOfSectionsInTableView(tableView: UITableView) -> Int {
        return count // 表示したいセルの数
    }
    func tableView(tableView: UITableView, cellForRowAtIndexPath indexPath: NSIndexPath) -> UITableViewCell {
        return cell // 表示したいセル
    }
}

Register as a new user and use Qiita more conveniently

  1. You get articles that match your needs
  2. You can efficiently read back useful information
  3. You can use dark theme
What you can do with signing up
47