#author("2016-05-24T15:52:07+09:00","default:wikiwriter","wikiwriter")
&tag(UIKit/テーブル);
*目次 [#sc69111a]
#contents
*関連ページ [#pe99027a]
-[[Storyboard]]


*参考情報 [#w10c3f1c]

*Single View ApplicationでUITableViewControllerから始める方法 [#n5114e1e]
*概要 [#e44ab7b1]
-Swiftでテーブルビューを実装する方法

*基礎: Single View ApplicationでUITableViewControllerから始める方法 [#n5114e1e]
-Single View Applicationを生成。
-UITableViewControllerのサブクラスを追加(ex: MyTableViewController)。
-Main.storyboardを開き、既存のView Controllerを削除。
-Table View Controllerをストーリーボードにドロップして追加。
-そのクラスをMyTableViewControllerとする。

*UITableViewを使って実装(ストーリーボード) [#o41cfc7a]
-該当するViewControllerをデータソース、Delegate先とする場合、以下のようになる。
#pre{{
class ViewController: UIViewController, UITableViewDataSource, UITableViewDelegate {
    
    @IBOutlet weak var tableView: UITableView!
    
    var objects = ["まぐろ", "サーモン", "えび", "はまち", "いか", "うなぎ"]
    
    override func viewDidLoad() {
        super.viewDidLoad()
        // Do any additional setup after loading the view, typically from a nib.
        self.navigationItem.rightBarButtonItem = self.editButtonItem()
        tableView.backgroundColor = UIColor.grayColor()
    }

    override func didReceiveMemoryWarning() {
        super.didReceiveMemoryWarning()
        // Dispose of any resources that can be recreated.
    }
    
    func tableView(tableView: UITableView, numberOfRowsInSection section: Int) -> Int {
        return objects.count
    }
    
    func tableView(tableView: UITableView, cellForRowAtIndexPath indexPath: NSIndexPath) -> UITableViewCell {
        let cell = UITableViewCell(style: UITableViewCellStyle.Default, reuseIdentifier: "myCell")
        cell.textLabel?.text = objects[indexPath.row]
        cell.backgroundColor = UIColor.brownColor()
        return cell
    }
    
    override func setEditing(editing: Bool, animated: Bool) {
        super.setEditing(editing, animated: animated)
        tableView.setEditing(editing, animated: animated)
        
    }
}
}}

**reason: 'unable to dequeue a cell with identifier Cell - must register a nib or a class for the identifier or connect a prototype cell in a storyboard'が発生 [#z9a077c2]
-メッセージ通りプロトタイプセルにidentifierが設定されていないせい。
-Table View ControllerのPrototype Cellsを選択し、Identity InspectorのIdentifierに"myCell"と設定する。


*Static Cellsを使用する [#r096f901]

**概要 [#y9b2f263]
-[[[iOS 8] storyboard で作成した Static Cells にコードからアクセスする | Developers.IO:http://dev.classmethod.jp/references/ios-8-staticcells/]]
-Static Cellsのテーブルを利用すると、ストーリーボードを使って行の定義ができて便利。
-ただしUITableViewControllerじゃないと使えない。

**基本的使用方法 [#x93c4998]
-Table View Controllerをストーリーボードにドロップ。Attributes InspectorでContentを「Static Cells」に変更する。
-内部のTable View Cellを選択し、LabelのTextをプレインに変更すると画面から行を設定できる。
-セクションの数や行の数はTable Viewや、Table View SectionのAttributes Inspectorで変更できる。

**クラスを割り当てる場合 [#k70bb7f3]
-UITableViewControllerのサブクラスを割り当てる場合、以下の部分はコメントアウトする(そうしないとストーリーボードの設定が無視されてしまう)。
#pre{{
    override func numberOfSectionsInTableView(tableView: UITableView!) -> Int {
        return 1
    }

    override func tableView(tableView: UITableView!, numberOfRowsInSection section: Int) -> Int {
        return self.items.count
    }

}}

*ステータスバー対応 [#m4a2d75b]
-何も考えずUITableViewControllerを実装するとステータスバーの下にセルが潜り込んでしまう。
-[[やはりお前らのiOS7対応は間違っている(解説編) - Qiita:http://qiita.com/yimajo/items/254c7cebab7864678246]]のはなしは関係なさそう。
-以下のようにcontentInsetを設定するしかないかも?ストーリーボードから設定する方法は不明。
#pre{{
    override func viewDidLoad() {
        super.viewDidLoad()
        self.tableView.contentInset = UIEdgeInsets(top: 20, left: 0, bottom: 0, right: 0)
        self.tableView.backgroundColor = UIColor.grayColor()
    }
}}
-ナビゲーションコントローラーを併用するとよろしくやってくれるので普通はそっちを使えばいいのかもしれない。


トップ   新規 一覧 検索 最終更新   ヘルプ   最終更新のRSS