LoginSignup
6
1

More than 5 years have passed since last update.

Swift UITableViewController セルが表示されない

Posted at

実装環境

Swift2.3
Xcode8
Siera バージョン 10.12

UITableViewControllerがうまく表示されない

もしかしたら自分と同じ原因でUITableViewControllerの実装が上手く行ってない人が居るかもしれないと思いメモしておきます。下記のコードが原因でセルが全然表示されなくて困り果てました

TestTableViewController.swift

override func numberOfSections(in tableView: UITableView) -> Int {

        return 0
        //↑これが原因
    }


override func numberOfSectionsとは?

下記に二つの画像があります。一つ目は自分が実装したUITableViewControllerです。二つ目の画像はiPhoneの設定画面です。

セクションとは、この「ラベル」から下のセルの集まりのことです。

自分はここを0のままにしていました。UITableViewControllerの雛型のファイルを挿入した時デフォルトであるコードです。

よく分からずいじらないままにしておいたのです。ちゃんとコードを全て理解しなかったことが原因で、困り果てることになりました。

下記のようにコード修正、もしくは削除することでセルはちゃんと表示されるようになりました。

正しいコードに修正(もしくはこのコードを消すとセルがちゃんと表示される)

TestTableViewController.swift

override func numberOfSections(in tableView: UITableView) -> Int {
        //numberOfSections"セクション"という意味シミュレーターの"ラベル"の部分です
        //ちなみにここのreturnを0にするとセルがセルが表示されない


        return 2
        //↑セクション「ラベル」を二つ出す
    }


シミュレーターで表示確認

スクリーンショット 2016-10-27 20.13.13.png

iPhoneでセクションを見てみる

130911gian_ios118_cs1e1_x1000.jpg

正しいコードの全部

TestTableViewController.swift

import UIKit

class TestTableViewController: UITableViewController {


    //表示データ
    var dataList = [["今日は楽しかった","今日はつまんなかった"],[ "今日はめっちゃ楽しかった","今日はめっちゃつまんなかった"]]

    override func viewDidLoad() {
        super.viewDidLoad()

         }

    override func didReceiveMemoryWarning() {
        super.didReceiveMemoryWarning()


    }



    override func numberOfSections(in tableView: UITableView) -> Int {
        //numberOfSections"セクション"という意味シミュレーターの"ラベル"の部分です
        //ちなみにここのreturnを0にするとセルがセルが表示されない


        return 2
        //リターンが2だからセルの"ラベル"が二つ表示されている
    }

    override func tableView(
        _ tableView: UITableView, numberOfRowsInSection section: Int) -> Int {
        // #warning Incomplete implementation, return the number of rows
        if(section == 0){
            return  dataList[0].count

        }else


            if(section == 1){
            return  dataList[1].count
        }
        return 0
        //return dataList[0].count + dataList[1].count
    }


    override func tableView(_ tableView: UITableView, titleForHeaderInSection section: Int) -> String? {
        return "ラベル"
    }

    override func tableView(_ tableView: UITableView, cellForRowAt indexPath: IndexPath) -> UITableViewCell {
        let cell = tableView.dequeueReusableCell(withIdentifier: "TestCell", for: indexPath)

       cell.textLabel?.text = dataList[indexPath.section][indexPath.row]

        print(dataList)

        return cell
    }

    override func tableView(_ tableView: UITableView, commit editingStyle: UITableViewCellEditingStyle, 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
       }    
   }
}

GitHubにソースを全部アップ

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