「Ios/swift/TableView/画像付き」の版間の差分
ナビゲーションに移動
検索に移動
| 38行目: | 38行目: | ||
==テーブルが含まれたViewControllerをNavigationViewControllerの下のPageViewControllerを使う場合は== | ==テーブルが含まれたViewControllerをNavigationViewControllerの下のPageViewControllerを使う場合は== | ||
テーブルをスクロールしたときにy起点がナビゲーションバーの下に沈む。 | |||
これを解決するためにはPageViewControllerの | |||
2016年6月27日 (月) 15:08時点における版
画像付きTableViewの作成
- sotryboardにTableViewを貼り付ける
- TableViewオブジェクトをViewControllerに紐付ける
- TableViewオブジェクトの中のTableViewCellにImageViewとLabelViewを貼り付ける
- TableViewCellのIdentifierを"CustomCell"とする。(Identityではないので気をつける)
- TableViewCellのClass名をを"CustomTableViewCell"とする
- CocoaTouchClassのファイルを作成し"CustomTableViewCell"の名前で以下のように編集。
- 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
}
}
- 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
}
- 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の