SwiftでUIViewにUITableViewを貼り付け
Qiitaでもよく似た記事がありますが、サンプルとして上げておきます。 よくわからないまま「nib but didn't get a UITableView.」と言われ続ける人が減りますように。
まずは親クラスとプロトコル
継承するのはUIViewControllerです。UITableViewControllerとするとエラーになります。
import UIKit class SampleUIViewController: UIViewController, UITableViewDelegate, UITableViewDataSource { //中略 }
デリゲートメソッドはoverrideしない
UITableViewControllerを継承した場合はoverrideですが、今回はプロトコルなので普通にfuncで定義します。 必須のデリゲートメソッドはこうですね。
func numberOfSectionsInTableView(tableView: UITableView) -> Int { // #warning Potentially incomplete method implementation. // Return the number of sections. return 1 } func tableView(tableView: UITableView, numberOfRowsInSection section: Int) -> Int { // #warning Incomplete method implementation. // Return the number of rows in the section. return self.logData!.menbers.count } func tableView(tableView: UITableView, cellForRowAtIndexPath indexPath: NSIndexPath) -> UITableViewCell { let cell = tableView.dequeueReusableCellWithIdentifier("membercell", forIndexPath: indexPath) as! UITableViewCell // Configure the cell... return cell }
datasource,delegateも忘れずに
storybord上でテーブルのdatasource,delegateをViewControllerにつなぐのを忘れないように。
これでとりあえず動くと思います。