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

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

新規登録して質問してみよう
ただいま回答率
85.48%
Xcode

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

Swift

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

Q&A

0回答

1167閲覧

日付の取得

退会済みユーザー

退会済みユーザー

総合スコア0

Xcode

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

Swift

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

0グッド

0クリップ

投稿2017/02/08 14:35

編集2017/02/09 11:28

イメージ説明

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}

実現したいこと
アラートテキストで入力した値が追加されるときにサブタイトルで日付と日時を表示出来るようにしたいです。

//
最近プログラムに興味を持ち勉強し始めた高校生なのでまだまだ知識がなく大雑把な質問ですがよろしくお願いします
出来ればコードを教えていただけると助かります

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

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

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

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

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

alg

2017/02/09 00:10

・タイトルについてですが、日付は習得するものではなく取得するものでは? ・コードブロックが効いていないようです。開始のバッククォート3つはありますが終了がないので、追加すると良いかと思います。
退会済みユーザー

退会済みユーザー

2017/02/09 01:55

指摘ありがとうございます。それとコードブロックってどういったものでしょうか?
x_kai

2017/02/09 02:13

コードブロックは、ソースコードをコードだと分かりやすくするために囲むものといったらいいですかね?コードを```(バッククオート3つ)で囲み、前後に改行をいれるか、コードを選択して「<code>」ボタンを押すとコードブロックになりますよ。
fuzzball

2017/02/09 02:22

閉じる方の ``` が抜けてますね。
guest

あなたの回答

tips

太字

斜体

打ち消し線

見出し

引用テキストの挿入

コードの挿入

リンクの挿入

リストの挿入

番号リストの挿入

表の挿入

水平線の挿入

プレビュー

まだ回答がついていません

会員登録して回答してみよう

アカウントをお持ちの方は

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

ただいまの回答率
85.48%

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

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

質問する

関連した質問