[Swifty]DataSourceのサブクラス | Cocoa練習帳

[Swifty]DataSourceのサブクラス

今回も辛いはず!

DataSourceを継承したHandDataSourceを作る。

class HandDataSource: DataSource {
    override init() {
        super.init()
    }
    
    override func tableView(_ tableView: UITableView, cellForRowAt indexPath: IndexPath) -> UITableViewCell {
        guard let cell = tableView.dequeueReusableCell(withIdentifier: "Cell", for: indexPath as IndexPath) as? CardCell else {
            return UITableViewCell()
        }
        let card = document.getItem(at: indexPath.row)
        cell.fillWith(card: card)
        return cell
    }
}

ハンドビューコントローラがこれを利用するようにする。

class HandVC: UITableViewController {
    private var dataSource = HandDataSource()
}

スーパークラスを変更する。

class DataSource: NSObject, UITableViewDataSource, SourceType {
    func tableView(_ tableView: UITableView, cellForRowAt indexPath: IndexPath) -> UITableViewCell {
        fatalError("This method must be overriden")
    }
}

継承されなかったら、エラーにするということだ。

ソースコード GitHubからどうぞ。
https://github.com/murakami/workbook/tree/master/ios/Hand - GitHub
関連情報 文化を調和させる
【Cocoa練習帳】 http://www.bitz.co.jp/weblog/
http://ameblo.jp/bitz/(ミラー・サイト)