swift
1import UIKit 2 3class ViewController: UIViewController,UITableViewDelegate,UITableViewDataSource { 4 5 @IBOutlet weak var segmentedControl: UISegmentedControl! 6 @IBOutlet weak var tableView: UITableView! 7 8 let name = ["cell add btr"]//["ipad","mac","iPhone"] 9 var subname = ["cell add btr"] 10 let subname2 = ["cell add btr"]//["one","two","three","four"] 11 let subname3 = ["cell add btr"]//["apple","peach","orrange"] 12 13 override func viewDidLoad() { 14 super.viewDidLoad() 15 tableView.delegate = self 16 tableView.dataSource = self 17 // Do any additional setup after loading the view, typically from a nib. 18 } 19 20 func tableView(_ tableView: UITableView, willSelectRowAt indexPath: IndexPath) -> IndexPath? { 21 return indexPath 22 } 23 24 func tableView(_ tableView: UITableView, numberOfRowsInSection section: Int) -> Int { 25 switch segmentedControl.selectedSegmentIndex { 26 case 0: 27 return name.count 28 case 1: 29 return subname.count 30 case 2: 31 return subname2.count 32 case 3: 33 return subname3.count 34 default: 35 break 36 } 37 38 return 0 39 } 40 func tableView(_ tableView: UITableView, cellForRowAt indexPath: IndexPath) -> UITableViewCell{ 41 let cell = tableView.dequeueReusableCell(withIdentifier: "cell", for: indexPath) 42 43 switch segmentedControl.selectedSegmentIndex { 44 case 0: 45 cell.textLabel?.text = name[indexPath.row] 46 cell.backgroundColor = UIColor.red 47 case 1: 48 cell.textLabel?.text = subname[indexPath.row] 49 cell.backgroundColor = UIColor.blue 50 case 2: 51 cell.textLabel?.text = subname2[indexPath.row] 52 cell.backgroundColor = UIColor.lightGray 53 case 3: 54 cell.textLabel?.text = subname2[indexPath.row] 55 cell.backgroundColor = UIColor.yellow 56 default: 57 break 58 } 59 60 return cell 61 } 62 63 func tableView(_ tableView: UITableView, didSelectRowAtIndexPath indexPath: IndexPath) -> IndexPath? { 64 65 let cell = tableView.cellForRow(at: indexPath) 66 67 //if cell?.textLabel?.text != name[indexPath.row]{ 68 if name[indexPath.row] != "cell add btr"{ 69 tableView.deselectRow(at: indexPath, animated: true) 70 tableView.reloadRows(at: [indexPath], with: .none) 71 return nil 72 }else{ 73 let alert = UIAlertController(title: "タイトル", message: "メッセージ", preferredStyle: .alert) 74 75 // OKボタンの設定 76 let okAction = UIAlertAction(title: "OK", style: .default, handler: { 77 (action:UIAlertAction!) -> Void in 78 79 // OKを押した時入力されていたテキストを表示 80 if let textFields = alert.textFields { 81 82 // アラートに含まれるすべてのテキストフィールドを調べる 83 for textField in textFields { 84 //indexPath.rowで1,2,3,4のような順になる 85 self.subname.insert(textField.text!, at:self.subname.count - 1) 86 } 87 self.tableView.reloadData() 88 89 } 90 }) 91 alert.addAction(okAction) 92 93 // キャンセルボタンの設定 94 let cancelAction = UIAlertAction(title: "Cancel", style: .cancel, handler: nil) 95 alert.addAction(cancelAction) 96 97 // テキストフィールドを追加 98 alert.addTextField(configurationHandler: {(textField: UITextField!) -> Void in 99 textField.placeholder = "テキスト" 100 }) 101 102 103 alert.view.setNeedsLayout() // シミュレータの種類によっては、これがないと警告が発生 104 105 // アラートを画面に表示 106 self.present(alert, animated: true, completion: nil) 107 } 108 return indexPath 109 110 111 } 112 113 @IBAction func segmentedChanged(_ sender: Any) { 114 tableView.reloadData() 115 } 116 override func didReceiveMemoryWarning() { 117 super.didReceiveMemoryWarning() 118 // Dispose of any resources that can be recreated. 119 } 120 121 122}
自分で出来たところ
segmentedControlを利用しtableViewをいくつかに分け、さらに[cell add bar]までは表示させることができました。
やりたいこと
[cell add bar]をタップ時にalert textを表示させ入力したものをcellに反映させたいです。
困っているところ
コードではcell add btrをタップ時にalert textを表示させるように書いてるのですが実際に実行してタップしてみるとタップしたcellの色は変化するのですがalert textが表示されなくて困っています。
回答1件
あなたの回答
tips
プレビュー