実現させたいこと
例)
図のようにアラートを追加した時に入力した内容とさらにcellを追加できるボタンを追加できるようにしたいです
自分でやってみたところ
swift
1import UIKit 2import CoreData 3 4class ViewController: UIViewController, UITableViewDataSource { 5 6 @IBOutlet weak var tableView: UITableView! 7 8 var people = [Person]() 9 var kei = ["追加ボタン"] 10 11 override func viewDidLoad() { 12 super.viewDidLoad() 13 14 15 title = "\"The List\"" 16 tableView.register(UITableViewCell.self, forCellReuseIdentifier: "Cell") 17 } 18 19 func tableView(_ tableVeiw: UITableView, numberOfRowsInSection section: Int) -> Int { 20 return people.count 21 //return kei.count 22 } 23 24 func tableView(_ tableView: UITableView, cellForRowAt indexPath: IndexPath) -> UITableViewCell { 25 let cell = tableView.dequeueReusableCell(withIdentifier: "Cell") 26 27 let person = people[indexPath.row] 28 29 cell!.textLabel!.text = person.value(forKey: "name") as? String 30 //cell!.textLabel!.text = kei.value(forkey: "name") as? String 31 32 return cell! 33 } 34 35 func tableView(_ tableView: UITableView, commit editingStyle: UITableViewCellEditingStyle, forRowAt indexPath: IndexPath) { 36 let context = (UIApplication.shared.delegate as! AppDelegate).persistentContainer.viewContext 37 38 if editingStyle == .delete{ 39 //self.tableView.setEditing(true, animated: true) 40 41 context.delete(people[indexPath.row]) 42 43 (UIApplication.shared.delegate as! AppDelegate).saveContext() 44 45 do { 46 people = try context.fetch(Person.fetchRequest()) 47 }catch let error as NSError { 48 print("Could not save \(error), \(error.userInfo)") 49 } 50 tableView.reloadData() 51 } 52 } 53 54 @IBAction func addName(_ sender: Any) { 55 let alert = UIAlertController(title: "New name", message: "Enter a new name", preferredStyle: .alert) 56 57 let saveAction = UIAlertAction(title: "Save", style: .default) { (action) in 58 let textField = alert.textFields?.first 59 self.saveName(name: textField!.text!) 60 self.tableView.reloadData() 61 } 62 63 let cancelAction = UIAlertAction(title: "Cancel", style: .cancel, handler: nil) 64 65 alert.addTextField(configurationHandler: nil) 66 alert.addAction(saveAction) 67 alert.addAction(cancelAction) 68 69 present(alert, animated: true, completion: nil) 70 } 71 72 func saveName(name: String){ 73 let context = (UIApplication.shared.delegate as! AppDelegate).persistentContainer.viewContext 74 75 let person = Person(entity: Person.entity(), insertInto: context) 76 77 person.setValue(name, forKey: "name") 78 79 do{ 80 try context.save() 81 people.append(person) 82 } catch let error as NSError { 83 print("Could not save \(error), \(error.userInfo)") 84 } 85 } 86 87 override func viewWillAppear(_ animated: Bool) { 88 super.viewWillAppear(animated) 89 90 let context = (UIApplication.shared.delegate as! AppDelegate).persistentContainer.viewContext 91 92 do{ 93 let result = try context.fetch(Person.fetchRequest()) 94 people = result as! [Person] 95 }catch let error as NSError { 96 print("Could not save \(error), \(error.userInfo)") 97 } 98 } 99 100 override func didReceiveMemoryWarning() { 101 super.didReceiveMemoryWarning() 102 } 103 104} 105 106
var people = Person
var kei = ["cell 追加"]
ここの部分をひとまとめにしたらアラートで記入した部分のcellと他に「追加ボタン」と固定したしたcellが追加されるとされるのではないかと思うのですがどうすればいいか分からなくて困っています
何かアドバイスかコードでもいいので教えていだきたいです。
回答1件
あなたの回答
tips
プレビュー