facebook twitter hatena line email

Ios/swift/TableView/画像付き

提供: 初心者エンジニアの簡易メモ
移動: 案内検索

画像付きTableViewの作成

  1. sotryboardにTableViewを貼り付ける
  2. TableViewオブジェクトをViewControllerに紐付ける
  3. TableViewオブジェクトの中のTableViewCellにImageViewとLabelViewを貼り付ける
  4. TableViewCellのIdentifierを"CustomCell"とする。(Identityではないので気をつける)
  5. TableViewCellのClass名をを"CustomTableViewCell"とする
  6. CocoaTouchClassのファイルを作成し"CustomTableViewCell"の名前で以下のように編集。
  7. labelとimageのオブジェクトの紐付けも行う。
class CustomTableViewCell: UITableViewCell {
   @IBOutlet weak var customLabelView: UILabel!
   @IBOutlet weak var customImageView: UIImageView!
   func setCell(imgpath: String, text: String) {
       customImageView.image = UIImage(named:imgpath)
       customLabelView.text = text
   }
}
  1. ViewControllerに以下を追加
   func tableView(tableView: UITableView, numberOfRowsInSection section: Int) -> Int {
       return 10
   }
   func tableView(tableView: UITableView, cellForRowAtIndexPath indexPath: NSIndexPath) -> UITableViewCell {
       let cell =
           tableView.dequeueReusableCellWithIdentifier("CustomCell") as! CustomTableViewCell
       cell.setCell("prof400.png", text: "hoge")
       return cell
   }
  1. tableViewのメソッドを使えるようにインターフェイスを実装する
-class ViewController: UIViewController
+class ViewController: UIViewController, UITableViewDataSource,UITableViewDelegate {

参考:http://yuu.1000quu.com/use_a_custom_cell_in_swift

テーブルをView全体に表示するには

ViewControllerのプロパティのUnder Top Barsのチェックを外す

参考:http://qiita.com/nagatasci/items/fb933ae11ee53b80e1fb

テーブルが含まれたViewControllerをNavigationViewControllerの下のPageViewControllerを使う場合は

テーブルをスクロールしたときにy起点がナビゲーションバーの下に沈む。

これを解決するためにはPageViewControllerのプロパティのUnder Top Barsのチェックを外す