UITableViewを使うときにあると便利なコードテンプレート

UITableViewControllerや、UITableViewを使う際、レイアウトの設定や削除、移動などの機能に対してのメソッドが用意されています。 テーブルビューを実装したら、以下のコードをコピペで貼っておくと、よりコーディングが早くなることでしょう。ぜひご利用ください。

※ 2020年4月時点でのswiftに対応しています。swift2や3での実装は「swift3 uitableview method」などのキーボードでgoogle検索しましょう。

  // セクションの数
  override func numberOfSections(in tableView: UITableView) -> Int {
    // #warning Incomplete implementation, return the number of sections
    return 1
  }
  // データの数
  override func tableView(_ tableView: UITableView, numberOfRowsInSection section: Int) -> Int {
    // #warning Incomplete implementation, return the number of rows
    return listData.count
  }
  // セルの高さ
  override func tableView(_ tableView: UITableView, heightForRowAt indexPath: IndexPath) -> CGFloat {
    return 44
  }
  // セルにデータを展開する
  override func tableView(_ tableView: UITableView, cellForRowAt indexPath: IndexPath) -> UITableViewCell {
    let cell = tableView.dequeueReusableCell(withIdentifier: "cell", for: indexPath)
    
    // Configure the cell...
    cell.textLabel?.text = listData[indexPath.row]
    
    return cell
  }
  // 各セルを選択した時に実行されるメソッド
  override func tableView(_ tableView: UITableView, didSelectRowAt indexPath: IndexPath) {
  }
  // 各セルを選択解除した時に実行されるメソッド
  override func tableView(_ tableView: UITableView, didDeselectRowAt indexPath: IndexPath) {
  }
  // セクションの高さを返す
  override func tableView(_ tableView: UITableView, heightForHeaderInSection section: Int) -> CGFloat {
    return 0
  }
  // フッターの高さを返す
  override func tableView(_ tableView: UITableView, heightForFooterInSection section: Int) -> CGFloat {
    return 0
  }
  // 各セクションのヘッダー文字列を返す
  override func tableView(_ tableView: UITableView, titleForHeaderInSection section: Int) -> String? {
    return ""
  }
  // 各セクションのフッター文字列を返す
  override func tableView(_ tableView: UITableView, titleForFooterInSection section: Int) -> String? {
    return ""
  }
  // セルが移動可能かどうか
  override func tableView(_ tableView: UITableView, canMoveRowAt indexPath: IndexPath) -> Bool {
    return true
  }
  // セルを異動したときに処理をするメソッド
  override func tableView(_ tableView: UITableView, moveRowAt fromIndexPath: IndexPath, to: IndexPath) {
    
  }
  // セルがフォーカス可能かどうか
  override func tableView(_ tableView: UITableView, canFocusRowAt indexPath: IndexPath) -> Bool {
    return false
  }
  // セルが編集可能かどうか
  override func tableView(_ tableView: UITableView, canEditRowAt indexPath: IndexPath) -> Bool {
    // Return false if you do not want the specified item to be editable.
    return true
  }
  // セルが削除が可能なことを伝えるメソッド
  override func tableView(_ tableView: UITableView, editingStyleForRowAt indexPath: IndexPath)-> UITableViewCell.EditingStyle {
    return UITableViewCell.EditingStyle.delete
  }
  // Delete ボタンが押された時に呼ばれるメソッド
  override func tableView(_ tableView: UITableView, commit editingStyle: UITableViewCell.EditingStyle, forRowAt indexPath: IndexPath) {
    // 削除
    if editingStyle == .delete {
      // Delete the row from the data source
      tableView.deleteRows(at: [indexPath], with: .fade)
    // 追加
    } else if editingStyle == .insert {
      // Create a new instance of the appropriate class, insert it into the array, and add a new row to the table view
    }
  }


Category