【Swift4】ToDoリストアプリを作りながらUITableViewを学ぼう!〜Part3 【説明動画付き】
Part2の続きです。part3ではUIButtonを押したらアラートが表示され、テキストフィールドでユーザーに書かれた文字をToDoリストに表示していくコードを書いていきます。また、スワイプしたらTableVieWのCellを消去する機能も追加していきます。UIAlertController,UIAlertAction,等が学べます
Contents
参考動画
新規アイテム追加機能
ToDoViewController.swiftに紐づける
storybord→プラスボタンをToDoViewControllerにコントロール&ドラッグ→actionを選択&ANYを選択
1 |
@IBAction func addButtonPressed(_ sender: Any) { |
が追加されるはずです。下記コードのように変更していきます
1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 |
@IBAction func addButtonPressed(_ sender: Any) { var textField = UITextField() let alert = UIAlertController(title: "アイテムを追加", message: "", preferredStyle: .alert) let action = UIAlertAction(title: "リストに追加", style: .default) { (action) in let newItem: Item = Item(title: textField.text!) self.itemArray.append(newItem) self.tableView.reloadData() } alert.addTextField { (alertTextField) in alertTextField.placeholder = "NEWアイテム" textField = alertTextField } alert.addAction(action) present(alert, animated: true, completion: nil) } |
tableViewのCellを消去する機能
下記コードを追加するとスワイプするとリストが消去されます。
1 2 3 4 5 6 |
override func tableView(_ tableView: UITableView, commit editingStyle: UITableViewCell.EditingStyle, forRowAt indexPath: IndexPath) { itemArray.remove(at: indexPath.row) let indexPaths = [indexPath] tableView.deleteRows(at: indexPaths, with: .automatic) } |
解説
UIAlertController
UIAlertControllerはアラートを出すパーツです。
UIAlertAction
styleはどのようなstyleで出すか指定してます。(style: .default)のdefaultをdestructive赤文字で否定的な選択肢やcancelに変えることもできます。
1 |
UIAlertAction(title: "リストに追加", style: .default) |
placeholder
テキストフィールド内に元々入っている薄い文字を指定してます。”NEWアイテム”を変更することにより好きな文字がいれれます。
1 2 3 4 |
alert.addTextField { (alertTextField) in alertTextField.placeholder = "NEWアイテム" textField = alertTextField } |
present
Apple公式developperに詳細が載っています。簡単に説明すると現在のViewの上に追加するという意味です。
1 |
present(alert, animated: true, completion: nil) |
実行結果
全体のソースコード
ToDoViewController.Swift
1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 23 24 25 26 27 28 29 30 31 32 33 34 35 36 37 38 39 40 41 42 43 44 45 46 47 48 49 50 51 52 53 54 55 56 57 58 59 60 61 62 63 64 65 66 67 68 69 70 71 72 73 74 75 76 77 78 79 80 81 82 83 84 85 86 87 88 89 90 91 92 93 94 95 96 97 98 99 100 101 102 103 104 105 106 |
import UIKit class ToDoViewController: UITableViewController { // アイテムの型 class Item { var title : String var done: Bool = false init(title: String) { self.title = title } } // この配列に作ったアイテムを追加していく var itemArray: [Item] = [] override func viewDidLoad() { super.viewDidLoad() // あらかじめ3つアイテムを作っておく let item1: Item = Item(title: "FLYーWESTに電話する") let item2: Item = Item(title: "フリーランスエンジニアになる") let item3: Item = Item(title: "稼いで幸せになる") // 先ほど作ったitemArray配列に追加 itemArray.append(item1) itemArray.append(item2) itemArray.append(item3) // NaviBarのタイトルを大きく表示させ、スクロールした場合は小さくする navigationController?.navigationBar.prefersLargeTitles = true } // セルの数を指定ーitemArrayの配列の数だけCellを表示します override func tableView(_ tableView: UITableView, numberOfRowsInSection section: Int) -> Int { return itemArray.count } // Cellの内容を決める override func tableView(_ tableView: UITableView, cellForRowAt indexPath: IndexPath) -> UITableViewCell { //part1で指定した「ToDoCell」を引っ張ってくる let cell = tableView.dequeueReusableCell(withIdentifier: "ToDoCell", for: indexPath) //Cell番号のitemArrayを変数Itemに代入 let item = itemArray[indexPath.row] //ToDoCellにCell番号のItemArrayの中身を表示させるようにしている cell.textLabel?.text = item.title //チェックマークを表示する処理ーitemのdoneがtrueだったら表示falseだったら非表示 cell.accessoryType = item.done ? .checkmark : .none return cell } // クリックしたらチェックマークが表示される機能 override func tableView(_ tableView: UITableView, didSelectRowAt indexPath: IndexPath) { // 選択されたセルにの中身を代入 let item = itemArray[indexPath.row] // falseだったらtrue、trueだったらfalseを代入 item.done = !item.done // リロードしてUIに反映 self.tableView.reloadData() } @IBAction func addButtonPressed(_ sender: Any) { var textField = UITextField() let alert = UIAlertController(title: "アイテムを追加", message: "", preferredStyle: .alert) let action = UIAlertAction(title: "リストに追加", style: .default) { (action) in let newItem: Item = Item(title: textField.text!) self.itemArray.append(newItem) self.tableView.reloadData() } alert.addTextField { (alertTextField) in alertTextField.placeholder = "NEWアイテム" textField = alertTextField } alert.addAction(action) present(alert, animated: true, completion: nil) } override func tableView(_ tableView: UITableView, commit editingStyle: UITableViewCell.EditingStyle, forRowAt indexPath: IndexPath) { itemArray.remove(at: indexPath.row) let indexPaths = [indexPath] tableView.deleteRows(at: indexPaths, with: .automatic) } } |
Item.swift
1 2 3 4 5 6 7 8 9 10 |
import Foundation class Item{ var title : String var done : Bool = false init(title:String) { self.title = title } } |
FLY-WESTプログラミングスクールでは格安で未経験からフリーランスエンジニアへ導きます!
