swift
1import UIKit 2import CoreData 3 4class ViewController: UIViewController, UITableViewDataSource { 5 6 @IBOutlet weak var tableView: UITableView! 7 8 var people = [Person]() 9 10 override func viewDidLoad() { 11 super.viewDidLoad() 12 13 14 title = "\"The List\"" 15 tableView.register(UITableViewCell.self, forCellReuseIdentifier: "Cell") 16 } 17 18 func tableView(_ tableVeiw: UITableView, numberOfRowsInSection section: Int) -> Int { 19 return people.count 20 } 21 22 func tableView(_ tableView: UITableView, cellForRowAt indexPath: IndexPath) -> UITableViewCell { 23 let cell = tableView.dequeueReusableCell(withIdentifier: "Cell") 24 25 let person = people[indexPath.row] 26 27 cell!.textLabel!.text = person.value(forKey: "name") as? String 28 29 return cell! 30 } 31 32 func tableView(_ tableView: UITableView, commit editingStyle: UITableViewCellEditingStyle, forRowAt indexPath: IndexPath) { 33 let context = (UIApplication.shared.delegate as! AppDelegate).persistentContainer.viewContext 34 35 if editingStyle == .delete{ 36 //self.tableView.setEditing(true, animated: true) 37 38 context.delete(people[indexPath.row]) 39 40 (UIApplication.shared.delegate as! AppDelegate).saveContext() 41 42 do { 43 people = try context.fetch(Person.fetchRequest()) 44 }catch let error as NSError { 45 print("Could not save \(error), \(error.userInfo)") 46 } 47 tableView.reloadData() 48 } 49 } 50 51 @IBAction func addName(_ sender: Any) { 52 let alert = UIAlertController(title: "New name", message: "Enter a new name", preferredStyle: .alert) 53 54 let saveAction = UIAlertAction(title: "Save", style: .default) { (action) in 55 let textField = alert.textFields?.first 56 self.saveName(name: textField!.text!) 57 self.tableView.reloadData() 58 } 59 60 let cancelAction = UIAlertAction(title: "Cancel", style: .cancel, handler: nil) 61 62 alert.addTextField(configurationHandler: nil) 63 alert.addAction(saveAction) 64 alert.addAction(cancelAction) 65 66 present(alert, animated: true, completion: nil) 67 } 68 69 func saveName(name: String){ 70 let context = (UIApplication.shared.delegate as! AppDelegate).persistentContainer.viewContext 71 72 let person = Person(entity: Person.entity(), insertInto: context) 73 74 person.setValue(name, forKey: "name") 75 76 do{ 77 try context.save() 78 people.append(person) 79 } catch let error as NSError { 80 print("Could not save \(error), \(error.userInfo)") 81 } 82 } 83 84 override func viewWillAppear(_ animated: Bool) { 85 super.viewWillAppear(animated) 86 87 let context = (UIApplication.shared.delegate as! AppDelegate).persistentContainer.viewContext 88 89 do{ 90 let result = try context.fetch(Person.fetchRequest()) 91 people = result as! [Person] 92 }catch let error as NSError { 93 print("Could not save \(error), \(error.userInfo)") 94 } 95 } 96 97 override func didReceiveMemoryWarning() { 98 super.didReceiveMemoryWarning() 99 } 100 101}
実現したいこと
アラートテキストで入力した値が追加されるときにサブタイトルで日付と日時を表示出来るようにしたいです。
//
最近プログラムに興味を持ち勉強し始めた高校生なのでまだまだ知識がなく大雑把な質問ですがよろしくお願いします
出来ればコードを教えていただけると助かります
あなたの回答
tips
プレビュー