LoginSignup
1
2

More than 5 years have passed since last update.

Swift備忘録~tableView編~

Last updated at Posted at 2018-01-25

はじめに

Story Boardを使わずにtableViewのセクションの実装を記録しました。
今回もあくまで自分が理解するために書いたものです。

環境

Xcode9.2

今回の備忘録

セクションの数の実装

extension exampleViewController: UITableViewDataSource {

    func numberOfSections(in tableView: UITableView) -> Int {
            return 2  //返したいセクションの数 2 を返す
    }

    func tableView(_ tableView: UITableView, heightForHeaderInSection section: Int) -> CGFloat {
            switch section {
        case 0:
                   return 2  //セクションの中のセルの数 2 を返す
        case 1:
                   return 1  //セクションの中のセルの数 1 を返す
        default:
                   return 0 //どれも該当しない場合なのでセルの数 0 を返す
        }
    }

    func tableView(_ tableView: UITableView, cellForRowAt indexPath: IndexPath) -> UITableViewCell {
            switch indexPath {
        case [0, 0]:
                   return ● // ● は返したいセルの内容
        case [0, 1]:
                   return ○ // ○ は返したいセルの内容
        case [1, 0]:
                   return ■ // ■ は返したいセルの内容
        default:
                   return UITableViewCell()
            }
    }
}

extension exampleViewController: UITableViewDelegate {

    func tableView(_ tableView: UITableView, heightForHeaderInSection section: Int) -> CGFloat {
        if section == 0 {
            return ○  // ヘッダーの高さ ○ を返す
        } else {
            return ●  // ヘッダーの高さ ● を返す
        }
    }

    func tableView(_ tableView: UITableView, heightForFooterInSection section: Int) -> CGFloat {
                if section == 0 {
            return ○  // フッターの高さ ○ を返す
        } else {
            return ●  // フッターの高さ ● を返す
        }
    }
    func tableView(_ tableView: UITableView, heightForRowAt indexPath: IndexPath) -> CGFloat {
        if indexPath.section == 1 {
            return 100
        }
            switch indexPath {
        case [0, 0]:
                   return 20 //セルの高さ 20 を返す
        case [0, 1]
                   return 40 //セルの高さ 40 を返す
        default:
                   return 0  //どれも該当しない場合なのでセルの高さは 0 を返す
            } 
    }
}
1
2
0

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
1
2