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になりました。
回答1件
あなたの回答
tips
プレビュー
バッドをするには、ログインかつ
こちらの条件を満たす必要があります。
退会済みユーザー
2018/05/03 07:51
2018/05/03 21:20
退会済みユーザー
2018/05/11 06:12 編集