「Ios/swift/TableView」の版間の差分
ナビゲーションに移動
検索に移動
編集の要約なし |
|||
| 35行目: | 35行目: | ||
@IBOutlet weak var customLabelView: UILabel! | @IBOutlet weak var customLabelView: UILabel! | ||
@IBOutlet weak var customImageView: UIImageView! | @IBOutlet weak var customImageView: UIImageView! | ||
func setCell(imgpath: String, text: String) { | |||
customImageView.image = UIImage(named:imgpath) | |||
customLabelView.text = text | |||
} | |||
} | } | ||
#ViewControllerに以下を追加 | #ViewControllerに以下を追加 | ||
| 43行目: | 47行目: | ||
let cell = | let cell = | ||
tableView.dequeueReusableCellWithIdentifier("CustomCell") as! CustomTableViewCell | tableView.dequeueReusableCellWithIdentifier("CustomCell") as! CustomTableViewCell | ||
cell. | cell.setCell("prof400.png", text: "hoge") | ||
return cell | return cell | ||
} | } | ||
2016年6月24日 (金) 20:36時点における版
storyboardを使ってTableViewを追加
- sotryboardにTableViewを貼り付ける
- TableViewオブジェクトをViewControllerに紐付ける
- 以下を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
}
- tableViewのメソッドを使えるようにインターフェイスを実装する
-class ViewController: UIViewController
+class ViewController: UIViewController, UITableViewDataSource,UITableViewDelegate {
参考:http://www.dcom-web.co.jp/technology/swift3/
画像付き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 {