facebook twitter hatena line email

「Ios/swift/TableView」の版間の差分

提供: 初心者エンジニアの簡易メモ
移動: 案内検索
(画像付きTableViewの作成)
行36: 行36:
 
     @IBOutlet weak var customImageView: UIImageView!
 
     @IBOutlet weak var customImageView: UIImageView!
 
  }
 
  }
 +
 +
参考:http://yuu.1000quu.com/use_a_custom_cell_in_swift

2016年6月23日 (木) 09:08時点における版

storyboardを使ってTableViewを追加

  1. sotryboardにTableViewを貼り付ける
  2. TableViewオブジェクトをViewControllerに紐付ける
  3. 以下をViewController.swiftに追加
   @IBOutlet weak var testTableView: UITableView!
   override func viewDidLoad() {
       super.viewDidLoad()
       testTableView.registerClass(UITableViewCell.self, forCellReuseIdentifier: "Cell")
       testTableView.delegate = self
       testTableView.dataSource = self
   }
   func tableView(tableView: UITableView, numberOfRowsInSection section: Int) -> Int {
       return 10
   }
   func tableView(tableView: UITableView, cellForRowAtIndexPath indexPath: NSIndexPath) -> UITableViewCell {
       let cell = tableView.dequeueReusableCellWithIdentifier("Cell", forIndexPath: indexPath) as! UITableViewCell
       cell.textLabel?.text = String(indexPath.row + 1) + "データ"
       return cell
   }
  1. クラスを継承する
-class ViewController: UIViewController
+class ViewController: UIViewController, UITableViewDataSource,UITableViewDelegate {

参考:http://www.dcom-web.co.jp/technology/swift3/

画像付きTableViewの作成

  1. sotryboardにTableViewを貼り付ける
  2. TableViewオブジェクトをViewControllerに紐付ける
  3. TableViewの中のTableViewCellにImageViewとLabelViewを貼り付ける
  4. TableViewCellのIdentityを"CustomCell"とし
  5. TableViewCellのClass名をを"CustomTableViewCell"とする
  6. CocoaTouchClassのファイルを作成し"CustomTableViewCell"の名前で以下のように編集。
  7. labelとimageのオブジェクトの紐付けも行う。
class CustomTableViewCell: UITableViewCell {
   @IBOutlet weak var customLabelView: UILabel!
   @IBOutlet weak var customImageView: UIImageView!
}

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