ベルマークのない自由帳

ベルマークのない自由帳

有益と無益の境界例

iOSアプリ開発メモ -UIViewControllerにUITableViewDataSourceを追加したらエラー

アプリ開発の勉強中に以下のようなクラス定義が登場。

class HogeLineViewController: UIViewController, UITableViewDelegate, UITableViewDataSource {
}

意味もよくわからずTableViewを追加したときの呪文だと思ってビルドしたらエラー。

Type ‘HogeViewController' does not conform to protocol ‘UITableViewDataSource’ 「HogeViewControllerがUITableViewDataSourceのプロトコルに従ってないよ」
ということでしょうか。
どういうことでしょうか、さっぱりわかりません。

解決策

以下の2つの関数を追加。

//セルに取得したものを貼り付ける。
func tableView(_ tableView: UITableView, cellForRowAt indexPath: IndexPath) -> UITableViewCell {
    }

//セクションの中にどれだけセルがあるか判定。
//`return hoge.count`とすれば取得したセルの数を返してくれる
func tableView(_ tableView: UITableView, numberOfRowsInSection section: Int) -> Int {
    }

参考にしたページ

mirai-stereo.net