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 //丸みに対して 38 label.layer.cornerRadius = 5 39 label.layer.masksToBounds = true 40 cell.contentView.addSubview(label) 41 42 label.translatesAutoresizingMaskIntoConstraints = false 43 cell.contentView.leftAnchor.constraint(equalTo: label.leftAnchor, constant: -6).isActive = true 44 cell.contentView.heightAnchor.constraint(equalTo: label.heightAnchor, multiplier: 1).isActive = true 45 46 //アラート 47 func addalert()-> String{ 48 let alert = UIAlertController(title: "タイトル", message: "メッセージ", preferredStyle: .alert) 49 50 // OKボタンの設定 51 let okAction = UIAlertAction(title: "OK", style: .default, handler: { 52 (action:UIAlertAction!) -> Void in 53 54 // OKを押した時入力されていたテキストを表示 55 if let textFields = alert.textFields { 56 57 // アラートに含まれるすべてのテキストフィールドを調べる 58 for textField in textFields { 59 60 self.item.insert(textField.text!, at: 0) 61 self.label.text = textField.text! 62 self.mytableView.insertRows(at: [IndexPath(row: 0, section: 0)],with: UITableViewRowAnimation.automatic) 63 print(textField.text!) 64 } 65 } 66 }) 67 alert.addAction(okAction) 68 69 // キャンセルボタンの設定 70 let cancelAction = UIAlertAction(title: "Cancel", style: .cancel, handler: nil) 71 alert.addAction(cancelAction) 72 73 // テキストフィールドを追加 74 alert.addTextField(configurationHandler: {(textField: UITextField!) -> Void in 75 textField.placeholder = "テキスト" 76 }) 77 78 alert.view.setNeedsLayout() // シミュレータの種類によっては、これがないと警告が発生 79 80 // アラートを画面に表示 81 self.present(alert, animated: true, completion: nil) 82 } 83 return cell 84 } 85 86@IBAction func addlabel(_ sender: Any) { 87 88 let key = addalert()//alertの値を渡したつもり 89 90 } 91 92 93 override func viewDidLoad() { 94 super.viewDidLoad() 95 96 } 97 98 99 100 override func didReceiveMemoryWarning() { 101 super.didReceiveMemoryWarning() 102 // Dispose of any resources that can be recreated. 103 } 104 105 106}
やりたいこと
ボタンをタップした時、アラートを呼び出したい。
困っていること
cellForRowAtの中にアラートを書いてしまった為にアラートを呼び出せなくなってしまった。
試したこと
func addalert()-> String{を関数として@IBAction func addlabel(_ sender: Any) {に値を渡そうとしているが大学のC言語で勉強した程度しか関数への理解が乏しいのでエラーが取れずに困っています
** エラーメッセージ**
Use of unresolved identifier 'addalert'
イメージ
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
回答3件
あなたの回答
tips
プレビュー