SwiftチュートリアルNo.4 Beginning iOS13&Swift App Development


今回は、 Chapter3 ToDoListです。
tableviewを使ったToDoアプリです。
この本の文章から、プログラムを入力していくのは、骨が折れます。注意するのは、”{” と “}” とが
正しく相対しているか、また誤入力していないか何度も確認する必要があります。
次回は、CoreDataを使ったToDoリストです。
// ViewController.swift
// TodoList
import UIKit
class ViewController: UIViewController,UITableViewDelegate,UITableViewDataSource {
func tableView(_ tableView: UITableView, numberOfRowsInSection section: Int) -> Int {
return Contents.count
}
func tableView(_ tableView: UITableView, cellForRowAt indexPath: IndexPath) -> UITableViewCell {
let TodoCell : UITableViewCell = tableView.dequeueReusableCell(withIdentifier: “TodoCell”, for: indexPath)
TodoCell.textLabel!.text = Contents[indexPath.row]
return TodoCell
}
override func viewDidLoad() {
super.viewDidLoad()
// Do any additional setup after loading the view.
if UserDefaults.standard.object(forKey: “TodoList”) != nil {
Contents = UserDefaults.standard.object(forKey: “TodoList”) as! [String]
}
}
override func didReceiveMemoryWarning() {
super.didReceiveMemoryWarning()
}
}
// AddController.swift
// TodoList
import UIKit
var Contents = [String]()
class AddController: UIViewController {
@IBOutlet weak var TodoTextField: UITextField!
@IBAction func TodoAddButton(_ sender: Any) {
Contents.append(TodoTextField.text!)
TodoTextField.text = “”
Contents.append(TodoTextField.text!)
TodoTextField.text = “”
UserDefaults.standard.set( Contents, forKey: “TodoList”)
}
override func viewDidLoad() {
super.viewDidLoad()
// Do any additional setup after loading the view.
}