Todoアプリの作成。一つのセルに複数の情報を表示させたい。
textFieldに入力された内容(タスク)を、セルに表示させるTodoアプリを作ることができ、そのタスクを「いつ開始して、いつ終了するのか」といった情報をUIDatePickerを使って取得したいと考えています。
①追加すべきソースコード ②storyboardに追加すべき要素
などを教えていただけますでしょうか。よろしくお願いします。
試したこと
UIDatePickerを2つ使って、cellに表示させようとしましたが、元々うまく実行できていたタスクの内容の表示もできなくなってしまいました。
ソースコードとstoryboard
######ViewController
import UIKit
//classの継承を追加
class ViewController: UIViewController, UITableViewDelegate, UITableViewDataSource {
//UITableView, numberOfRowsInSectionの追加(表示するcell数を決める) func tableView(_ tableView: UITableView, numberOfRowsInSection section: Int) -> Int { //戻り値の設定(表示するcell数) return TodoKobetsunonakami.count } //UITableView, cellForRowAtの追加(表示するCELLの中身を決める) func tableView(_ tableView: UITableView, cellForRowAt indexPath: IndexPath) -> UITableViewCell { //変数を作る let TodoCell: UITableViewCell = tableView.dequeueReusableCell(withIdentifier: "TodoCell", for: indexPath) //変数の中身を作る TodoCell.textLabel!.text = TodoKobetsunonakami[indexPath.row] //戻り値の設定(表示する中身) return TodoCell } //UITableView, cellForRowAtの削除 func tableView(_ tableView: UITableView, commit editingStyle: UITableViewCell.EditingStyle, forRowAt indexPath: IndexPath) { let index = indexPath.row TodoKobetsunonakami.remove(at: index) tableView.reloadData() } override func viewDidLoad() { super.viewDidLoad() //追加画面で入力した内容を取得する if UserDefaults.standard.object(forKey: "TodoList") != nil { TodoKobetsunonakami = UserDefaults.standard.object(forKey: "TodoList") as! [String] } // Do any additional setup after loading the view, typically from a nib. }
}
######AddController
import UIKit
//変数の設置(TODO個別の中身)
var TodoKobetsunonakami = String
class AddController: UIViewController {
//テキストフィールドの設定 @IBOutlet weak var TodoTextField: UITextField! //追加ボタンの設定 @IBAction func TodoAddButton(_ sender: Any) { //変数に入力内容を入れる TodoKobetsunonakami.append(TodoTextField.text!) //追加ボタンを押したらフィールドを空にする TodoTextField.text = "" //変数の中身をUDに追加 UserDefaults.standard.set(TodoKobetsunonakami, forKey: "TodoList") } override func viewDidLoad() { super.viewDidLoad() // Do any additional setup after loading the view. } override func touchesBegan(_ touches: Set<UITouch>, with event: UIEvent?) { self.view.endEditing(true) } /* // MARK: - Navigation // In a storyboard-based application, you will often want to do a little preparation before navigation override func prepare(for segue: UIStoryboardSegue, sender: Any?) { // Get the new view controller using segue.destination. // Pass the selected object to the new view controller. } */
補足情報
Xcodeのバージョン: 4.2.1
swiftのバージョン: version 10.1
あなたの回答
tips
プレビュー