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

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

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

iOSとは、Apple製のスマートフォンであるiPhoneやタブレット端末のiPadに搭載しているオペレーションシステム(OS)です。その他にもiPod touch・Apple TVにも搭載されています。

Xcode

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

Swift

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

iPhone

iPhoneとは、アップル社が開発・販売しているスマートフォンです。 同社のデジタルオーディオプレーヤーiPodの機能、電話機能、インターネットやメールなどのWeb通信機能の3つをドッキングした機器です。

Q&A

解決済

1回答

1462閲覧

tableViewのcellについて

退会済みユーザー

退会済みユーザー

総合スコア0

iOS

iOSとは、Apple製のスマートフォンであるiPhoneやタブレット端末のiPadに搭載しているオペレーションシステム(OS)です。その他にもiPod touch・Apple TVにも搭載されています。

Xcode

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

Swift

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

iPhone

iPhoneとは、アップル社が開発・販売しているスマートフォンです。 同社のデジタルオーディオプレーヤーiPodの機能、電話機能、インターネットやメールなどのWeb通信機能の3つをドッキングした機器です。

0グッド

1クリップ

投稿2018/05/01 01:29

編集2018/05/02 03:28

swift4

swift

1import UIKit 2 3class ViewController: UIViewController,UITableViewDataSource,UITableViewDelegate{ 4 5 @IBOutlet weak var mytableView: UITableView! 6 var item = [String]() 7 8 func tableView(_ tableView: UITableView, numberOfRowsInSection section: Int) -> Int { 9 return item.count 10 } 11 12 func tableView(_ tableView: UITableView, cellForRowAt indexPath: IndexPath) -> UITableViewCell { 13 let cell = tableView.dequeueReusableCell(withIdentifier: "cell", for: indexPath) 14 let todoLabel = item[indexPath.row] 15 cell.textLabel?.text = todoLabel 16 17 //ボタンについて 18 let button = UIButton() 19 button.backgroundColor = UIColor.blue 20 button.setTitle(" 追 加 ", for: .normal) 21 cell.contentView.addSubview(button) 22 23 button.translatesAutoresizingMaskIntoConstraints = false 24 cell.contentView.rightAnchor.constraint(equalTo: button.rightAnchor, constant: 12).isActive = true 25 // 中央にする 26 //button.centerYAnchor.constraint(equalTo: cell.centerYAnchor).isActive = true 27 //丸みに対して 28 button.layer.cornerRadius = 10 29 button.layer.masksToBounds = true 30 31 cell.contentView.heightAnchor.constraint(equalTo: button.heightAnchor, multiplier: 1).isActive = true 32 33 //ラベルについて 34 var label = UILabel() 35 label.backgroundColor = UIColor.darkGray 36 label.text = "120×10" 37 // itemに格納した値を設定 38 if indexPath.row < item.count { 39 label.text = item[indexPath.row] 40 } 41 42 //丸みに対して 43 label.layer.cornerRadius = 5 44 label.layer.masksToBounds = true 45 cell.contentView.addSubview(label) 46 47 label.translatesAutoresizingMaskIntoConstraints = false 48 cell.contentView.leftAnchor.constraint(equalTo: label.leftAnchor, constant: -6).isActive = true 49 cell.contentView.heightAnchor.constraint(equalTo: label.heightAnchor, multiplier: 1).isActive = true 50 51 //アラート 52 func addalert()-> String{ 53 let alert = UIAlertController(title: "タイトル", message: "メッセージ", preferredStyle: .alert) 54 55 // OKボタンの設定 56 let okAction = UIAlertAction(title: "OK", style: .default, handler: { 57 (action:UIAlertAction!) -> Void in 58 59 // OKを押した時入力されていたテキストを表示 60 if let textFields = alert.textFields { 61 62 // アラートに含まれるすべてのテキストフィールドを調べる 63 for textField in textFields { 64 65 self.item.insert(textField.text!, at: 0) 66 self.label.text = textField.text! 67 self.mytableView.insertRows(at: [IndexPath(row: 0, section: 0)],with: UITableViewRowAnimation.automatic) 68 print(textField.text!) 69 } 70 } 71 }) 72 alert.addAction(okAction) 73 74 // キャンセルボタンの設定 75 let cancelAction = UIAlertAction(title: "Cancel", style: .cancel, handler: nil) 76 alert.addAction(cancelAction) 77 78 // テキストフィールドを追加 79 alert.addTextField(configurationHandler: {(textField: UITextField!) -> Void in 80 textField.placeholder = "テキスト" 81 }) 82 83 alert.view.setNeedsLayout() // シミュレータの種類によっては、これがないと警告が発生 84 85 // アラートを画面に表示 86 self.present(alert, animated: true, completion: nil) 87 } 88 return cell 89 } 90 91@IBAction func addlabel(_ sender: Any) { 92 93 alert() 94 95 96 } 97 98 99 override func viewDidLoad() { 100 super.viewDidLoad() 101 102 } 103 104 105 106 override func didReceiveMemoryWarning() { 107 super.didReceiveMemoryWarning() 108 } 109 110 111}

やりたい事

alertを使って一個ずつlabelを挿入できるようにしたい

出来ている事
横一列にセルを挿入出来た。

困っていること
alert一回で五つ同じlabelが挿入されてしまう。
どうしたら一個ずつlabelを挿入できるのかが分からなくて困っています。

swift

1 //labelインスタンス 2 //label1 3 let label = UILabel() 4 label.backgroundColor = UIColor.darkGray 5 //label2 6 let label2 = UILabel() 7 label2.backgroundColor = UIColor.darkGray 8 //label3 9 let label3 = UILabel() 10 label3.backgroundColor = UIColor.darkGray 11 //label4 12 let label4 = UILabel() 13 label4.backgroundColor = UIColor.darkGray 14 //label5 15 let label5 = UILabel() 16 label5.backgroundColor = UIColor.darkGray 17 18 //丸みに対して 19 //label1 20 label.layer.cornerRadius = 5 21 label.layer.masksToBounds = true 22 label.sizeToFit() 23 label.translatesAutoresizingMaskIntoConstraints = false 24 cell.contentView.addSubview(label) 25 //label2 26 label2.layer.cornerRadius = 5 27 label2.layer.masksToBounds = true 28 label2.sizeToFit() 29 label2.translatesAutoresizingMaskIntoConstraints = false 30 cell.contentView.addSubview(label2) 31 //label3 32 label3.layer.cornerRadius = 5 33 label3.layer.masksToBounds = true 34 label3.sizeToFit() 35 label3.translatesAutoresizingMaskIntoConstraints = false 36 cell.contentView.addSubview(label3) 37 //label4 38 label4.layer.cornerRadius = 5 39 label4.layer.masksToBounds = true 40 label4.sizeToFit() 41 label4.translatesAutoresizingMaskIntoConstraints = false 42 cell.contentView.addSubview(label4) 43 //label5 44 label5.layer.cornerRadius = 5 45 label5.layer.masksToBounds = true 46 label5.sizeToFit() 47 label5.translatesAutoresizingMaskIntoConstraints = false 48 cell.contentView.addSubview(label5) 49 50 //座標的な 51 cell.contentView.leftAnchor.constraint(equalTo: label.leftAnchor, constant: -6).isActive = true 52 cell.contentView.leftAnchor.constraint(equalTo: label2.leftAnchor, constant: -70).isActive = true 53 cell.contentView.leftAnchor.constraint(equalTo: label3.leftAnchor, constant: -134).isActive = true 54 cell.contentView.leftAnchor.constraint(equalTo: label4.leftAnchor, constant: -198).isActive = true 55 cell.contentView.leftAnchor.constraint(equalTo: label5.leftAnchor, constant: -262).isActive = true 56 57 cell.contentView.heightAnchor.constraint(equalTo: label.heightAnchor, multiplier: 1).isActive = true 58 cell.contentView.heightAnchor.constraint(equalTo: label2.heightAnchor, multiplier: 1).isActive = true 59 cell.contentView.heightAnchor.constraint(equalTo: label3.heightAnchor, multiplier: 1).isActive = true 60 cell.contentView.heightAnchor.constraint(equalTo: label4.heightAnchor, multiplier: 1).isActive = true 61 cell.contentView.heightAnchor.constraint(equalTo: label5.heightAnchor, multiplier: 1).isActive = true 62 63 // itemに格納した値を設定 64 if indexPath.row < add.count { 65 label.text = add[indexPath.row] 66 label2.text = add[indexPath.row] 67 label3.text = add[indexPath.row] 68 label4.text = add[indexPath.row] 69 label5.text = add[indexPath.row] 70 } 71 72 73func alert(){ 74 // テキストフィールド付きアラート表示 75 76 let alert = UIAlertController(title: "タイトル", message: "メッセージ", preferredStyle: .alert) 77 78 // OKボタンの設定 79 let okAction = UIAlertAction(title: "OK", style: .default, handler: { 80 (action:UIAlertAction!) -> Void in 81 82 // OKを押した時入力されていたテキストを表示 83 if let textFields = alert.textFields { 84 85 // アラートに含まれるすべてのテキストフィールドを調べる 86 for textField in textFields { 87 if self.cnt < 1{ 88 self.item1.insert(textField.text!, at: 0) 89 self.mytableView.insertRows(at: [IndexPath(row: 0, section: 0)],with: UITableViewRowAnimation.automatic) 90 }else if self.cnt < 2{ 91 self.item2.insert(textField.text!, at: 0) 92 self.mytableView.insertRows(at: [IndexPath(row: 0, section: 0)],with: UITableViewRowAnimation.automatic) 93 }else if self.cnt < 3{ 94 self.item3.insert(textField.text!, at: 0) 95 self.mytableView.insertRows(at: [IndexPath(row: 0, section: 0)],with: UITableViewRowAnimation.automatic) 96 }else if self.cnt < 4{ 97 self.item4.insert(textField.text!, at: 0) 98 self.mytableView.insertRows(at: [IndexPath(row: 0, section: 0)],with: UITableViewRowAnimation.automatic) 99 }else if self.cnt < 5{ 100 self.item5.insert(textField.text!, at: 0) 101 self.mytableView.insertRows(at: [IndexPath(row: 0, section: 0)],with: UITableViewRowAnimation.automatic) 102 } 103 //print(textField.text!) 104 //self.Gesture() 105 //self.doubleclic() 106 } 107 } 108 }) 109 alert.addAction(okAction) 110 111 // キャンセルボタンの設定 112 let cancelAction = UIAlertAction(title: "Cancel", style: .cancel, handler: nil) 113 alert.addAction(cancelAction) 114 115 // テキストフィールドを追加 116 alert.addTextField(configurationHandler: {(textField: UITextField!) -> Void in 117 textField.placeholder = "テキスト" 118 }) 119 120 alert.view.setNeedsLayout() // シミュレータの種類によっては、これがないと警告が発生 121 122 // アラートを画面に表示 123 self.present(alert, animated: true, completion: nil) 124 125 }

alertで入力し挿入するタイミングでsignalsigabrtになりました。

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

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

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

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

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

guest

回答1

0

ベストアンサー

あまり上手い方法が思いつかなかったのですが、一応下記のような形でできました。

※あくまでイメージの参考程度にしてください。

import UIKit class ViewController: UIViewController,UITableViewDataSource,UITableViewDelegate{ @IBOutlet weak var mytableView: UITableView! var item: [String] = [] var rowCount = 0 var itemTotal: [[String]] = [[]] func tableView(_ tableView: UITableView, numberOfRowsInSection section: Int) -> Int { if (itemTotal.count == 0 || itemTotal.count > 0 && itemTotal[0].count == 0) { return 0 } else { return itemTotal.count } } //アラート func addalert() { let alert = UIAlertController(title: "タイトル", message: "メッセージ", preferredStyle: .alert) // OKボタンの設定 let okAction = UIAlertAction(title: "OK", style: .default, handler: { (action:UIAlertAction!) -> Void in // OKを押した時入力されていたテキストを表示 if let textFields = alert.textFields { // アラートに含まれるすべてのテキストフィールドを調べる for textField in textFields { self.item.insert(textField.text!, at: 0) self.itemTotal[self.rowCount] = self.item if self.item.count % 5 == 0, self.itemTotal[0].count > 1 { self.rowCount += 1 self.itemTotal.append([]) self.item = [] } self.mytableView.reloadData() } } }) alert.addAction(okAction) // キャンセルボタンの設定 let cancelAction = UIAlertAction(title: "Cancel", style: .cancel, handler: nil) alert.addAction(cancelAction) // テキストフィールドを追加 alert.addTextField(configurationHandler: {(textField: UITextField!) -> Void in textField.placeholder = "テキスト" }) alert.view.setNeedsLayout() // シミュレータの種類によっては、これがないと警告が発生 // アラートを画面に表示 self.present(alert, animated: true, completion: nil) } func tableView(_ tableView: UITableView, cellForRowAt indexPath: IndexPath) -> UITableViewCell { let cell = UITableViewCell() let index = itemTotal.count - 1 - indexPath.row let item = itemTotal[index] guard item.count > 0 else { return cell } //let todoLabel = item[indexPath.row] //cell.textLabel?.text = todoLabel //ボタンについて let button = UIButton() button.backgroundColor = UIColor.blue button.setTitle(" 追 加 ", for: .normal) cell.contentView.addSubview(button) button.translatesAutoresizingMaskIntoConstraints = false cell.contentView.rightAnchor.constraint(equalTo: button.rightAnchor, constant: 12).isActive = true // 中央にする //button.centerYAnchor.constraint(equalTo: cell.centerYAnchor).isActive = true //丸みに対して button.layer.cornerRadius = 10 button.layer.masksToBounds = true cell.contentView.heightAnchor.constraint(equalTo: button.heightAnchor, multiplier: 1).isActive = true //ラベルについて var labels: [UILabel] = [] for i in item { let label = UILabel() label.backgroundColor = UIColor.darkGray label.text = i //丸みに対して label.layer.cornerRadius = 5 label.layer.masksToBounds = true cell.contentView.addSubview(label) labels.append(label) } var preLabel: UILabel? = nil for l in labels { l.translatesAutoresizingMaskIntoConstraints = false if (preLabel == nil) { l.leftAnchor.constraint(equalTo: cell.contentView.leftAnchor, constant: 12).isActive = true } else { l.leftAnchor.constraint(equalTo: preLabel!.rightAnchor, constant: 20).isActive = true } cell.contentView.heightAnchor.constraint(equalTo: l.heightAnchor, multiplier: 1).isActive = true preLabel = l } return cell } @IBAction func addlabel(_ sender: Any) { addalert() } override func viewDidLoad() { super.viewDidLoad() mytableView.dataSource = self } }

投稿2018/05/02 22:24

newmt

総合スコア1277

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

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

退会済みユーザー

退会済みユーザー

2018/05/03 07:51

ありがとうございます。質問の内容以上のことを教えて頂きありがとうございました。 ちなみに何ですがlabelをサイズを予め指定して、labelのサイズに合わせてフォントの大きさを変えたい場合はどうしたら良いのでしょうか? 下記のコードでフォントサイズは調整出来るようになったのですがlabelの大きさの設定の仕方が分からなくて困っています。 frameで書くと座標指定もしなきゃいけなくなるので何か良い方法はないでしょうか? label.adjustsFontSizeToFitWidth = true label.minimumScaleFactor = 10.0
退会済みユーザー

退会済みユーザー

2018/05/11 06:12 編集

度々お忙しい中すいません。 わからない部分があるのでもし良ければ教えて頂けませんか? https://teratail.com/questions/125651
guest

あなたの回答

tips

太字

斜体

打ち消し線

見出し

引用テキストの挿入

コードの挿入

リンクの挿入

リストの挿入

番号リストの挿入

表の挿入

水平線の挿入

プレビュー

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

ただいまの回答率
85.48%

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

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

質問する

関連した質問