質問をすることでしか得られない、回答やアドバイスがある。

15分調べてもわからないことは、質問しよう!

新規登録して質問してみよう
ただいま回答率
85.48%
Xcode

Xcodeはソフトウェア開発のための、Appleの統合開発環境です。Mac OSXに付随するかたちで配布されています。

Swift

Swiftは、アップルのiOSおよびOS Xのためのプログラミング言語で、Objective-CやObjective-C++と共存することが意図されています

Q&A

解決済

2回答

5363閲覧

【Swift / CustomセルのLabelにコードでデータを入れたい】

NobumitsuHata

総合スコア141

Xcode

Xcodeはソフトウェア開発のための、Appleの統合開発環境です。Mac OSXに付随するかたちで配布されています。

Swift

Swiftは、アップルのiOSおよびOS Xのためのプログラミング言語で、Objective-CやObjective-C++と共存することが意図されています

0グッド

1クリップ

投稿2016/03/20 05:35

storybord

swift

1import UIKit 2 3class TableViewController: UITableViewController { 4 5 6 override func viewDidLoad() { 7 super.viewDidLoad() 8 9 tableView.backgroundColor = UIColor.redColor() 10 11 self.title = "ore" 12 13 // Uncomment the following line to preserve selection between presentations 14 // self.clearsSelectionOnViewWillAppear = false 15 16 // Uncomment the following line to display an Edit button in the navigation bar for this view controller. 17 // self.navigationItem.rightBarButtonItem = self.editButtonItem() 18 } 19 20 override func didReceiveMemoryWarning() { 21 super.didReceiveMemoryWarning() 22 // Dispose of any resources that can be recreated. 23 } 24 25 // MARK: - Table view data source 26 27 override func numberOfSectionsInTableView(tableView: UITableView) -> Int { 28 // #warning Incomplete implementation, return the number of sections 29 return 1 30 } 31 32 override func tableView(tableView: UITableView, numberOfRowsInSection section: Int) -> Int { 33 // #warning Incomplete implementation, return the number of rows 34 return 1 35 } 36 37 /* 38 override func tableView(tableView: UITableView, cellForRowAtIndexPath indexPath: NSIndexPath) -> UITableViewCell { 39 let cell = tableView.dequeueReusableCellWithIdentifier("reuseIdentifier", forIndexPath: indexPath) 40 41 // Configure the cell... 42 43 return cell 44 } 45 */ 46 47 /* 48 // Override to support conditional editing of the table view. 49 override func tableView(tableView: UITableView, canEditRowAtIndexPath indexPath: NSIndexPath) -> Bool { 50 // Return false if you do not want the specified item to be editable. 51 return true 52 } 53 */ 54 55 /* 56 // Override to support editing the table view. 57 override func tableView(tableView: UITableView, commitEditingStyle editingStyle: UITableViewCellEditingStyle, forRowAtIndexPath indexPath: NSIndexPath) { 58 if editingStyle == .Delete { 59 // Delete the row from the data source 60 tableView.deleteRowsAtIndexPaths([indexPath], withRowAnimation: .Fade) 61 } else if editingStyle == .Insert { 62 // Create a new instance of the appropriate class, insert it into the array, and add a new row to the table view 63 } 64 } 65 */ 66 67 /* 68 // Override to support rearranging the table view. 69 override func tableView(tableView: UITableView, moveRowAtIndexPath fromIndexPath: NSIndexPath, toIndexPath: NSIndexPath) { 70 71 } 72 */ 73 74 /* 75 // Override to support conditional rearranging of the table view. 76 override func tableView(tableView: UITableView, canMoveRowAtIndexPath indexPath: NSIndexPath) -> Bool { 77 // Return false if you do not want the item to be re-orderable. 78 return true 79 } 80 */ 81 82 /* 83 // MARK: - Navigation 84 85 // In a storyboard-based application, you will often want to do a little preparation before navigation 86 override func prepareForSegue(segue: UIStoryboardSegue, sender: AnyObject?) { 87 // Get the new view controller using segue.destinationViewController. 88 // Pass the selected object to the new view controller. 89 } 90 */ 91 92} 93

storybordにあるラベル2つを上記のファイルにoutletしたとして、
よくみるcell.textLabel.text = ""というような書き方ができないと思います。
この場合にセルの中のラベルに値を入れる書き方ってどうなりますか?

サブクラスでUITableViewCellを継承して、そっちにoutletしろみたいなこともありましたが、
うまくいきません。

気になる質問をクリップする

クリップした質問は、後からいつでもMYページで確認できます。

またクリップした質問に回答があった際、通知やメールを受け取ることができます。

バッドをするには、ログインかつ

こちらの条件を満たす必要があります。

guest

回答2

0

ベストアンサー

高さを100に固定しているのでAutolayoutは考慮してませんが最低限の言っていることはできると思います。

TableViewControllerクラス

swift

1import UIKit 2 3class TableViewController: UITableViewController { 4 5 // 表示用データ 6 typealias Datatype = [(label1: String,label2: String)] 7 var data: Datatype = [("label1-1", "label1-2"),("label2-1", "label2-2"),("label3-1", "label3-2")] 8 9 override func viewDidLoad() { 10 super.viewDidLoad() 11 } 12 13 override func tableView(tableView: UITableView, numberOfRowsInSection section: Int) -> Int { 14 return data.count 15 } 16 17 override func tableView(tableView: UITableView, cellForRowAtIndexPath indexPath: NSIndexPath) -> UITableViewCell { 18 19 // IdentifierをStoryboardで設定したCustomCellにして CustomCellクラスを生成する 20 let cell = tableView.dequeueReusableCellWithIdentifier("CustomCell", forIndexPath: indexPath) as! CustomCell 21 22 // CustomCellに配置したラベルにテキストを設定 23 cell.label1.text = data[indexPath.row].label1 24 cell.label2.text = data[indexPath.row].label2 25 return cell 26 } 27 28 override func tableView(tableView: UITableView, heightForRowAtIndexPath indexPath: NSIndexPath) -> CGFloat { 29 return 100 30 } 31}

CustomCell クラス

swift

1import UIKit 2 3class CustomCell: UITableViewCell { 4 5 // 以下の2つがStoryboardのlabelと紐付いています 6 @IBOutlet weak var label1: UILabel! 7 @IBOutlet weak var label2: UILabel! 8 9 override func awakeFromNib() { 10 super.awakeFromNib() 11 } 12 13 override func setSelected(selected: Bool, animated: Bool) { 14 super.setSelected(selected, animated: animated) 15 } 16}

image

image2

シュミレータースクショ
image3

投稿2016/03/20 14:49

編集2016/03/20 15:17
_Kentarou

総合スコア8490

バッドをするには、ログインかつ

こちらの条件を満たす必要があります。

Stripe

2016/03/21 03:16

CustomCellで、awakeFromNib()とsetSelected()をオーバーライドしている理由は何ですか?
_Kentarou

2016/03/21 03:26

UITableViewCellクラスを継承して作成した時にDefaultで作成されたものです。 そのままで消してないだけです。
NobumitsuHata

2016/03/21 06:59

祝日にもかかわらず丁寧なご回答ありがとうございます! ここまでいければ、自分であとはレイアウトの調節するだけなのでとても助かりました。 ベストアンサーにさせていただきます。
guest

0

Outletでうまくいかないなら、tagでも使ってみては?

Swift

1if let label = cell.contentView.viewWithTag(tag) as? UILabel { 2 label.text = "hoge" 3}

投稿2016/03/20 08:55

Stripe

総合スコア2183

バッドをするには、ログインかつ

こちらの条件を満たす必要があります。

あなたの回答

tips

太字

斜体

打ち消し線

見出し

引用テキストの挿入

コードの挿入

リンクの挿入

リストの挿入

番号リストの挿入

表の挿入

水平線の挿入

プレビュー

15分調べてもわからないことは
teratailで質問しよう!

ただいまの回答率
85.48%

質問をまとめることで
思考を整理して素早く解決

テンプレート機能で
簡単に質問をまとめる

質問する

関連した質問