swift
1import UIKit 2import CoreData 3 4class ViewController: UIViewController, UITableViewDataSource { 5 6 @IBOutlet weak var tableView: UITableView! 7 8 var data = NSData() 9 var people = [Person]() 10 11 override func viewDidLoad() { 12 super.viewDidLoad() 13 14 title = "\"The List\"" 15 //tableView.register(UITableViewCell.self, forCellReuseIdentifier: "TestCell") 16 self.data = NSData() 17 } 18 19 func tableView(_ tableVeiw: UITableView, numberOfRowsInSection section: Int) -> Int { 20 return people.count 21 } 22 23 func tableView(_ tableView: UITableView, cellForRowAt indexPath: IndexPath) -> UITableViewCell { 24 let cell = tableView.dequeueReusableCell(withIdentifier: "TestCell") 25 26 let person = people[indexPath.row] 27 28 cell!.textLabel!.text = person.value(forKey: "name") as? String 29 30 //subtitle 現在日時を取得 31 let now = NSDate() // 現在日時の取得 32 33 let dateFormatter = DateFormatter() 34 dateFormatter.locale = NSLocale(localeIdentifier: "en_US") as Locale! // ロケールの設定 35 dateFormatter.dateFormat = "yyyy年MM月dd日 HH:mm"//:ss" // 日付フォーマットの設定 36 37 let dateString = dateFormatter.string(from: now as Date) 38 print(dateString) // -> 2014/06/25 02:13:18 39 cell!.detailTextLabel?.text = dateString 40 //cell!.detailTextLabel?.text = person.value(forKey: "Subtitle") as? String 41 return cell! 42 } 43 44 func tableView(_ tableView: UITableView, commit editingStyle: UITableViewCellEditingStyle, forRowAt indexPath: IndexPath) { 45 let context = (UIApplication.shared.delegate as! AppDelegate).persistentContainer.viewContext 46 47 if editingStyle == .delete{ 48 //self.tableView.setEditing(true, animated: true) 49 50 context.delete(people[indexPath.row]) 51 52 (UIApplication.shared.delegate as! AppDelegate).saveContext() 53 54 do { 55 people = try context.fetch(Person.fetchRequest()) 56 }catch let error as NSError { 57 print("Could not save \(error), \(error.userInfo)") 58 } 59 tableView.reloadData() 60 } 61 } 62 63 @IBAction func addName(_ sender: Any) { 64 let alert = UIAlertController(title: "New name", message: "Enter a new name", preferredStyle: .alert) 65 66 let saveAction = UIAlertAction(title: "Save", style: .default) { (action) in 67 let textField = alert.textFields?.first 68 self.saveName(name: textField!.text!) 69 self.tableView.reloadData() 70 } 71 72 let cancelAction = UIAlertAction(title: "Cancel", style: .cancel, handler: nil) 73 74 alert.addTextField(configurationHandler: nil) 75 alert.addAction(saveAction) 76 alert.addAction(cancelAction) 77 78 present(alert, animated: true, completion: nil) 79 80 } 81 82 func saveName(name: String){ 83 let context = (UIApplication.shared.delegate as! AppDelegate).persistentContainer.viewContext 84 85 let person = Person(entity: Person.entity(), insertInto: context) 86 87 88 person.setValue(name, forKey: "name") 89 90 do{ 91 try context.save() 92 people.append(person) 93 } catch let error as NSError { 94 print("Could not save \(error), \(error.userInfo)") 95 } 96 } 97 98 override func viewWillAppear(_ animated: Bool) { 99 super.viewWillAppear(animated) 100 101 let context = (UIApplication.shared.delegate as! AppDelegate).persistentContainer.viewContext 102 103 do{ 104 let result = try context.fetch(Person.fetchRequest()) 105 people = result as! [Person] 106 }catch let error as NSError { 107 print("Could not save \(error), \(error.userInfo)") 108 } 109 } 110 111 override func didReceiveMemoryWarning() { 112 super.didReceiveMemoryWarning() 113 } 114 115}
実現したいこと
アラートテキストで入力した時の最終更新時間をsubtitleに表示したい
困ってること
subtitleに現在の年月日時間(例)2017年2月18日 12:00)までは表示させることが出来たが時間が進むにつれsutitleの年月日時間も更新されるのでそれをアラートテキストで入力した時の最終更新時間で止めたいです。
自分で試してみたところ
var data = NSData() //viewdidLoad self.data = NSData()
を追加してみたが出来ませんでした。
*core dataを使っています
色々自分なりに試してみたのですがどうしても解決出来なかったので教えて頂けたいです。
回答1件
あなたの回答
tips
プレビュー
バッドをするには、ログインかつ
こちらの条件を満たす必要があります。
退会済みユーザー
2017/02/18 14:27
退会済みユーザー
2017/02/19 12:53 編集
退会済みユーザー
2017/02/20 01:07
退会済みユーザー
2017/02/20 01:44
2017/02/20 04:18
退会済みユーザー
2017/02/22 14:41