swift
1//realm.class 2 3import RealmSwift 4 5class TopTodo: Object{ 6 // 管理用 ID。プライマリーキー 7 @objc dynamic var id = 0 8 9 ///TopView 10 @objc dynamic var item = "" 11 /// 最終更新日時 12 @objc dynamic var date = NSData() 13} 14
swift
1//viewcontller 2 3class TopViewViewController: UIViewController,UITableViewDataSource,UITableViewDelegate { 4 5 6 @IBOutlet weak var TopViewTabelView: UITableView! 7 8 let ToDo = TopTodo() 9 var todoItem:Results<TopTodo>! 10 11 12 13 override func viewDidLoad() { 14 super.viewDidLoad() 15 16 17 navigationItem.title = "履歴" 18 // 永続化されているデータを取りだす 19 let realm = try! Realm() 20 self.todoItem = realm.objects(TopTodo.self) 21 self.TopViewTabelView.reloadData() 22 23 24 //navigationController?.navigationBar.prefersLargeTitles = true //これがやりたかった設定やメモ帳のナビゲーションの可変のやつ 25 navigationItem.title = "データベース" 26 TopViewTabelView.tableFooterView = UIView(frame: .zero) 27 self.view.backgroundColor = UIColor.rgb(r: 250, g: 204, b: 125, alpha: 1) 28 TopViewTabelView.frame = CGRect(x: 0, y: 64, width: 375, height: 667) 29 30 } 31 32 33 34 35 @IBAction func addBtr(_ sender: Any) { 36 let alert = UIAlertController(title: "タイトル", message: "メッセージ", preferredStyle: .alert) 37 38 // OKボタンの設定 39 let okAction = UIAlertAction(title: "OK", style: .default, handler: { 40 (action:UIAlertAction!) -> Void in 41 42 // OKを押した時入力されていたテキストを表示 43 if let textFields = alert.textFields { 44 45 // アラートに含まれるすべてのテキストフィールドを調べる 46 for textField in textFields { 47 //self.item.insert(textField.text!, at: 0) 48 49 let newTodo = TopTodo() 50 newTodo.item = textField.text! 51 52 let realm = try! Realm() 53 54 // トランザクション開始 55 try! realm.write({ () -> Void in 56 realm.add(newTodo) 57 print("ToDo Saved") 58 }) 59 // ここでトランザクションが終了 60 61 // ここでtodoItemの更新 62 self.todoItem = realm.objects(TopTodo.self) 63 self.TopViewTabelView.insertRows(at: [IndexPath(row: 0, section: 0)],with: UITableViewRowAnimation.automatic) 64 print(textField.text!) 65 66 } 67 self.TopViewTabelView.reloadData() 68 } 69 }) 70 alert.addAction(okAction) 71 72 // キャンセルボタンの設定 73 let cancelAction = UIAlertAction(title: "Cancel", style: .cancel, handler: nil) 74 alert.addAction(cancelAction) 75 76 // テキストフィールドを追加 77 alert.addTextField(configurationHandler: {(textField: UITextField!) -> Void in 78 textField.placeholder = "テキスト" 79 }) 80 alert.view.setNeedsLayout() // シミュレータの種類によっては、これがないと警告が発生 81 82 self.present(alert, animated: true, completion: nil) 83 } 84 85 func tableView(_ tableView: UITableView, numberOfRowsInSection section: Int) -> Int { 86 return todoItem.count 87 88 } 89 90 func tableView(_ tableView: UITableView, cellForRowAt indexPath: IndexPath) -> UITableViewCell { 91 let Cell = tableView.dequeueReusableCell(withIdentifier: "cell", for: indexPath) 92 93 let object = todoItem[indexPath.row] 94 Cell.textLabel?.text = object.item 95 Cell.detailTextLabel?.text = object.date.description 96 97 return Cell 98 } 99 100 func tableView(_ tableView: UITableView, commit editingStyle: UITableViewCellEditingStyle, forRowAt indexPath: IndexPath) { 101 102 103 if(editingStyle == UITableViewCellEditingStyle.delete) { 104 105 let realm = try! Realm() 106 try! realm.write { 107 realm.delete(self.todoItem[indexPath.row]) 108 } 109 TopViewTabelView.deleteRows(at: [indexPath], with: UITableViewRowAnimation.fade) 110 TopViewTabelView.reloadData() 111 } 112 } 113 114} 115
実現したい事
cellが追加した際にcellが追加された日時をサブタイトルに表示させたい
気になること
例えばcellが追加した時が2018 4/1 11 AMだとして追加され、後日に修正を加えた時に
Apple //タイトル
2018 4/1 11 AM 最新更新 2018 4/3 3 PM //サブタイトル
このような形に出来るのでしょうか?
試したこと
Cell.detailTextLabel?.text = object.date
ですと型が違うというエラーが出るので
Cell.detailTextLabel?.text = object.date.description
このようにしてみたがそうするとサブタイトルの表示が**<>**このような表示になってしまいます。
追記
swift
1 func tableView(_ tableView: UITableView, cellForRowAt indexPath: IndexPath) -> UITableViewCell { 2 let Cell = tableView.dequeueReusableCell(withIdentifier: "cell", for: indexPath) 3 4 let object = todoItem[indexPath.row] 5 //let now = object.date// // 現在日時の取得 6 7 let dateFormatter = DateFormatter() 8 dateFormatter.locale = NSLocale(localeIdentifier: "en_US") as Locale? // ロケールの設定 9 dateFormatter.dateFormat = "yyyy年MM月dd日 HH:mm"//:ss" // 日付フォーマットの設定 10 11 let dateString = dateFormatter.string(from: object.date as Date) 12 print(dateString) // -> 2014/06/25 02:13:18*/ 13 14 15 Cell.textLabel?.text = object.item 16 Cell.detailTextLabel?.text = object.date.description 17 18 19 return Cell 20 21 22 }
回答1件
あなたの回答
tips
プレビュー
バッドをするには、ログインかつ
こちらの条件を満たす必要があります。
退会済みユーザー
2018/06/28 00:42
2018/06/28 01:49
退会済みユーザー
2018/06/28 02:06