質問をすることでしか得られない、回答やアドバイスがある。

15分調べてもわからないことは、質問しよう!

新規登録して質問してみよう
ただいま回答率
85.50%
Core Data

Core DataはAppleのOS X and iOSのためのオブジェクトモデリングと持続性を持ったフレームワークです。Xcodeはエンティティー、属性そして関係性を特定するためのオブジェクトモデルの編集機能を提供します。

TableView

TableView(UITableView)とは、リスト形式で表示するコントロールで、ほとんどのアプリに使用されています。画面を「行」に分けて管理し、一般的には各行をタップした際に詳細画面に移動します。

iOS

iOSとは、Apple製のスマートフォンであるiPhoneやタブレット端末のiPadに搭載しているオペレーションシステム(OS)です。その他にもiPod touch・Apple TVにも搭載されています。

Xcode

Xcodeはソフトウェア開発のための、Appleの統合開発環境です。Mac OSXに付随するかたちで配布されています。

Swift

Swiftは、アップルのiOSおよびOS Xのためのプログラミング言語で、Objective-CやObjective-C++と共存することが意図されています

Q&A

解決済

1回答

1616閲覧

tableview 更新時間

退会済みユーザー

退会済みユーザー

総合スコア0

Core Data

Core DataはAppleのOS X and iOSのためのオブジェクトモデリングと持続性を持ったフレームワークです。Xcodeはエンティティー、属性そして関係性を特定するためのオブジェクトモデルの編集機能を提供します。

TableView

TableView(UITableView)とは、リスト形式で表示するコントロールで、ほとんどのアプリに使用されています。画面を「行」に分けて管理し、一般的には各行をタップした際に詳細画面に移動します。

iOS

iOSとは、Apple製のスマートフォンであるiPhoneやタブレット端末のiPadに搭載しているオペレーションシステム(OS)です。その他にもiPod touch・Apple TVにも搭載されています。

Xcode

Xcodeはソフトウェア開発のための、Appleの統合開発環境です。Mac OSXに付随するかたちで配布されています。

Swift

Swiftは、アップルのiOSおよびOS Xのためのプログラミング言語で、Objective-CやObjective-C++と共存することが意図されています

0グッド

0クリップ

投稿2017/02/18 06:20

編集2017/02/18 14:21

イメージ説明

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を使っています
色々自分なりに試してみたのですがどうしても解決出来なかったので教えて頂けたいです。

イメージ説明

気になる質問をクリップする

クリップした質問は、後からいつでもMYページで確認できます。

またクリップした質問に回答があった際、通知やメールを受け取ることができます。

バッドをするには、ログインかつ

こちらの条件を満たす必要があります。

guest

回答1

0

ベストアンサー

Personというモデルがあると思いますが、そちらに作成年月日を保存するプロパティを追加してnameと一緒に保存してそれを表示すれば良いと思いますがいかがでしょうか?

投稿2017/02/18 06:40

_Kentarou

総合スコア8490

バッドをするには、ログインかつ

こちらの条件を満たす必要があります。

退会済みユーザー

退会済みユーザー

2017/02/18 14:27

とりあえずPersonのattributes中にnameの他にnowtimeを追加したのですがプロパティの追加とはこういうことでしょうか? またプロパティーを追加したらsaveNameの部分を書き換えればいいのしょうか? まだ勉強し始めたばかりであまり用語について詳しくないのでもう少し分かりやすように説明してもらえたら嬉しいです。
退会済みユーザー

退会済みユーザー

2017/02/19 12:53 編集

すいませんがどうしても分からないのでもう少しヒントでもいいので教えていただけませんか? 多分ですけどsaveNameのところのdo文を書き換えるんですよね? 再三しつこいと思いますがお願いします
退会済みユーザー

退会済みユーザー

2017/02/20 01:07

一つだけ教えて欲しいのですがPersonの他にエンティティをもう1つ作ればいいのでしょうか? それともattributes のnameの他にもう一つstring型の何かを作ればいいのでしょうか?
退会済みユーザー

退会済みユーザー

2017/02/20 01:44

func tableView(_ tableView: UITableView, cellForRowAt indexPath: IndexPath) -> UITableViewCell { let cell = tableView.dequeueReusableCell(withIdentifier: "Cell") let person = people[indexPath.row] cell!.textLabel!.text = person.value(forKey: "name") as? String //subtitle 現在日時を取得 //let now = people[indexPath.row] let now = NSDate() // 現在日時の取得 let dateFormatter = DateFormatter() dateFormatter.locale = NSLocale(localeIdentifier: "en_US") as Locale! // ロケールの設定 dateFormatter.dateFormat = "yyyy年MM月dd日 HH:mm"//:ss" // 日付フォーマットの設定 let dateString = dateFormatter.string(from: now as Date) print(dateString) // -> 2014/06/25 02:13:18 //cell!.detailTextLabel?.text = dateString cell!.detailTextLabel?.text = person.value(forKey: "dateString") as? String return cell! // cell!.textLabel!.text = person.value(forKey: "name") as? String テキストラベルの部分を真似てdetailTextLabelも同じようにしてみたのですがうまく表示できなかったのですが書き換える部分が違うのでしょうか?
_Kentarou

2017/02/20 04:18

文字列は保存できているのでしたら同じ要領で時間を保存して取得すればよいと思いますよ。 一旦アプリを削除してからもう一度実行してみてください。
退会済みユーザー

退会済みユーザー

2017/02/22 14:41

一つだけお聞きしたのですが cell!.detailTextLabel?.text = dateString にするとエラーが起きないのですが cell!.detailTextLabel?.text = person.value(forKey: "dateString") as? String にするとsignal SIGABRTになってどこでエラーが起きているのか探しても見つからないのですがこの場合やはり根本的にこのコードが間違っているのでしょうか?
guest

あなたの回答

tips

太字

斜体

打ち消し線

見出し

引用テキストの挿入

コードの挿入

リンクの挿入

リストの挿入

番号リストの挿入

表の挿入

水平線の挿入

プレビュー

15分調べてもわからないことは
teratailで質問しよう!

ただいまの回答率
85.50%

質問をまとめることで
思考を整理して素早く解決

テンプレート機能で
簡単に質問をまとめる

質問する

関連した質問