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 title = "\"The List\"" 14 tableView.register(UITableViewCell.self, forCellReuseIdentifier: "Cell") 15 } 16 17 func tableView(_ tableVeiw: UITableView, numberOfRowsInSection section: Int) -> Int { 18 return people.count 19 } 20 21 func tableView(_ tableView: UITableView, cellForRowAt indexPath: IndexPath) -> UITableViewCell { 22 let cell = tableView.dequeueReusableCell(withIdentifier: "Cell") 23 24 let person = people[indexPath.row] 25 26 cell!.textLabel!.text = person.value(forKey: "name") as? String 27 28 //subtitle 現在日時を取得 29 let now = NSDate() // 現在日時の取得 30 31 let dateFormatter = DateFormatter() 32 dateFormatter.locale = NSLocale(localeIdentifier: "en_US") as Locale! // ロケールの設定 33 dateFormatter.dateFormat = "yyyy年MM月dd日 HH:mm"//:ss" // 日付フォーマットの設定 34 35 let dateString = dateFormatter.string(from: now as Date) 36 print(dateString) // -> 2014/06/25 02:13:18 37 cell!.detailTextLabel?.text = person.value(forKey: "lastname") as? String 38 return cell! 39 } 40 41 func tableView(_ tableView: UITableView, commit editingStyle: UITableViewCellEditingStyle, forRowAt indexPath: IndexPath) { 42 let context = (UIApplication.shared.delegate as! AppDelegate).persistentContainer.viewContext 43 44 if editingStyle == .delete{ 45 //self.tableView.setEditing(true, animated: true) 46 47 context.delete(people[indexPath.row]) 48 49 (UIApplication.shared.delegate as! AppDelegate).saveContext() 50 51 do { 52 people = try context.fetch(Person.fetchRequest()) 53 }catch let error as NSError { 54 print("Could not save \(error), \(error.userInfo)") 55 } 56 tableView.reloadData() 57 } 58 } 59 60 @IBAction func addName(_ sender: Any) { 61 let alert = UIAlertController(title: "New name", message: "Enter a new name", preferredStyle: .alert) 62 63 let saveAction = UIAlertAction(title: "Save", style: .default) { (action) in 64 let textField = alert.textFields?.first 65 self.saveName(name: textField!.text!) 66 self.tableView.reloadData() 67 } 68 69 let cancelAction = UIAlertAction(title: "Cancel", style: .cancel, handler: nil) 70 71 alert.addTextField(configurationHandler: nil) 72 alert.addAction(saveAction) 73 alert.addAction(cancelAction) 74 75 present(alert, animated: true, completion: nil) 76 77 } 78 79 func saveName(name: String){ 80 let context = (UIApplication.shared.delegate as! AppDelegate).persistentContainer.viewContext 81 82 let person = Person(entity: Person.entity(), insertInto: context) 83 84 person.setValue(name, forKey: "name") 85 86 do{ 87 try context.save() 88 people.append(person) 89 } catch let error as NSError { 90 print("Could not save \(error), \(error.userInfo)") 91 } 92 } 93 94 override func viewWillAppear(_ animated: Bool) { 95 super.viewWillAppear(animated) 96 97 let context = (UIApplication.shared.delegate as! AppDelegate).persistentContainer.viewContext 98 99 do{ 100 let result = try context.fetch(Person.fetchRequest()) 101 people = result as! [Person] 102 }catch let error as NSError { 103 print("Could not save \(error), \(error.userInfo)") 104 } 105 } 106 107 override func didReceiveMemoryWarning() { 108 super.didReceiveMemoryWarning() 109 } 110 111}
試したこと
アラートテキストで入力したい値をtitleに表示できるようにし、subtitleで現在日時を自動的に取得しsubtitleにアラートテキストで入力した時間を表示させようとしたが実行してみるとデバッグエリアでしか表示されてなかった
実現したいこと
例)title 今日の出来事
subtitle 2017年2月15日 15:00
例のようにsubtitleで入力した現在日時を表示出来るようにしたいです。
説明が下手ですいませんが教えて頂けたら嬉しいです。
回答3件
あなたの回答
tips
プレビュー