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

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

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

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

Swift

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

Q&A

解決済

1回答

1612閲覧

UITableViewのセルにチェックをできるようにしたい

oilfriedchicken

総合スコア18

iOS

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

Swift

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

0グッド

0クリップ

投稿2018/11/28 02:52

編集2018/11/28 02:57

初心者です。よろしくお願いいたします。

前提・実現したいこと

swift3を使って本を参考に簡単なTodoリストを作成しています。
その中でUITableViewのセルにチェックを入れられるようにしたいです。

発生している問題・エラーメッセージ

しかし本通りコードを模写したのですがチェックされません
以下ソースコードになります。

swift

1class ViewController: UIViewController, UITableViewDataSource, UITabBarDelegate { 2 3 var todoList = [MyTodo]() 4 5 @IBOutlet weak var tableView: UITableView! 6 7 override func viewDidLoad() { 8 super.viewDidLoad() 9 let userDefaults = UserDefaults.standard 10 if let storedTodoList = userDefaults.object(forKey: "todoList") as? Data{ 11 if let unarchiveTodoList = NSKeyedUnarchiver.unarchiveObject( 12 with: storedTodoList) as? [MyTodo]{ 13 todoList.append(contentsOf: unarchiveTodoList) 14 } 15 } 16 } 17 18 override func didReceiveMemoryWarning() { 19 super.didReceiveMemoryWarning() 20 } 21 22 23 @IBAction func tapAddButton(_ sender: Any) { 24 25 let alertController = UIAlertController(title: "TODO追加", 26 message: "TODOを入力してください",preferredStyle:UIAlertControllerStyle.alert) 27 alertController.addTextField(configurationHandler: nil) 28 29 let okAction = UIAlertAction(title: "OK",style: UIAlertActionStyle.default) { (action:UIAlertAction) in 30 if let textField = alertController.textFields?.first{ 31 32 let myTodo = MyTodo() 33 myTodo.todoTitle = textField.text! 34 self.todoList.insert(myTodo, at:0) 35 36 self.tableView.insertRows(at: [IndexPath(row: 0, section: 0)], with: UITableViewRowAnimation.right) 37 38 let userDefaults = UserDefaults.standard 39 let data = NSKeyedArchiver.archivedData(withRootObject: self.todoList) 40 userDefaults.set(data, forKey: "todoList") 41 userDefaults.synchronize() 42 } 43 } 44 45 46 alertController.addAction(okAction) 47 48 let cancelButton = UIAlertAction(title: "CANCEL",style: UIAlertActionStyle.cancel, handler: nil) 49 alertController.addAction(cancelButton) 50 51 present(alertController, animated: true,completion: nil) 52 53 } 54 func tableView(_ tableView: UITableView, numberOfRowsInSection section: Int) -> Int { 55 return todoList.count 56 } 57 58 func tableView(_ tableView: UITableView, cellForRowAt indexPath: IndexPath) -> UITableViewCell { 59 let cell = tableView.dequeueReusableCell(withIdentifier: "todoCell", for: indexPath) 60 let myTodo = todoList[indexPath.row] 61 cell.textLabel?.text = myTodo.todoTitle 62 if myTodo.todoDone { 63 cell.accessoryType = UITableViewCellAccessoryType.checkmark 64 } else { 65 cell.accessoryType = UITableViewCellAccessoryType.none 66 } 67 return cell 68 } 69 70 func tableView(_ tableView: UITableView, 71 didSelectRowAt indexPath: IndexPath) { 72 let myTodo = todoList[indexPath.row] 73 if myTodo.todoDone{ 74 myTodo.todoDone = false 75 } else { 76 myTodo.todoDone = true 77 } 78 tableView.reloadRows(at: [indexPath], with: UITableViewRowAnimation.fade) 79 let data: Data = NSKeyedArchiver.archivedData(withRootObject: todoList) 80 let userDefaults = UserDefaults.standard 81 userDefaults.set(data, forKey: "todoList") 82 userDefaults.synchronize() 83 } 84 85 func tableView(_ tableView: UITableView, commit editingStyle:UITableViewCellEditingStyle, 86 forRowAt indexPath: IndexPath) { 87 if editingStyle == UITableViewCellEditingStyle.delete{ 88 todoList.remove(at: indexPath.row) 89 tableView.deleteRows(at: [indexPath], with: UITableViewRowAnimation.fade) 90 let data: Data = NSKeyedArchiver.archivedData(withRootObject: todoList) 91 let userDefaults = UserDefaults.standard 92 userDefaults.set(data, forKey: "todoList") 93 userDefaults.synchronize() 94 } 95 96 } 97 98} 99 100class MyTodo: NSObject,NSCoding { 101 var todoTitle: String? 102 var todoDone: Bool = false 103 override init(){ 104 } 105 106 required init?(coder aDecoder: NSCoder){ 107 todoTitle = aDecoder.decodeObject(forKey: "todoTitle") as? String 108 todoDone = aDecoder.decodeBool(forKey: "todoDone") 109 } 110 func encode(with aCoder: NSCoder) { 111 aCoder.encode(todoTitle, forKey: "todoTitle") 112 aCoder.encode(todoDone, forKey: "todoDone") 113 } 114} 115

該当のソースコード

チェック機能以外の部分に関しては実装できているのでチェックをつける部分の実装コードである、以下の部分が怪しいのかなと思いました。

swift

1func tableView(_ tableView: UITableView, cellForRowAt indexPath: IndexPath) -> UITableViewCell { 2 let cell = tableView.dequeueReusableCell(withIdentifier: "todoCell", for: indexPath) 3 let myTodo = todoList[indexPath.row] 4 cell.textLabel?.text = myTodo.todoTitle 5 if myTodo.todoDone { 6 cell.accessoryType = UITableViewCellAccessoryType.checkmark 7 } else { 8 cell.accessoryType = UITableViewCellAccessoryType.none 9 } 10 return cell 11 } 12 13 func tableView(_ tableView: UITableView, 14 didSelectRowAt indexPath: IndexPath) { 15 let myTodo = todoList[indexPath.row] 16 if myTodo.todoDone{ 17 myTodo.todoDone = false 18 } else { 19 myTodo.todoDone = true 20 }

試したこと

デバック等してみたのですがメソッドが呼び出されている様子でもないためErrorがないため困っています。

補足情報(FW/ツールのバージョンなど)

swift 3
xcode 8.2.1
参考にしている本:本気ではじめるiPhoneアプリ作り 黒帯エンジニアがしっかり教える基本テクニック Xcode8.X+Swift3.X対応

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

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

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

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

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

guest

回答1

0

ベストアンサー

セルをタップしたときのイベント(func tableView(_ tableView: UITableView, didSelectRowAt indexPath: IndexPath))はUITableViewDelegateプロトコルを経由して呼ばれます。

おそらくですが、

class ViewController: UIViewController, UITableViewDataSource, UITabBarDelegate

class ViewController: UIViewController, UITableViewDataSource, UITableViewDelegate, UITabBarDelegate

とし、
ストーリーボードの方で、テーブルビューのdelegateをViewControllerに接続するなりすれば良いかと思います。

この辺、本にも解説があると思いますのでUITableViewDelegate辺りが書いてないか読み直してみてください。

投稿2018/11/28 05:24

takabosoft

総合スコア8356

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

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

oilfriedchicken

2018/11/28 07:17

無事解決いたしました! 間違えてUITabBarDelegateと入力していたようです、丁寧に解説していただきありがとうございます!
guest

あなたの回答

tips

太字

斜体

打ち消し線

見出し

引用テキストの挿入

コードの挿入

リンクの挿入

リストの挿入

番号リストの挿入

表の挿入

水平線の挿入

プレビュー

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

ただいまの回答率
85.48%

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

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

質問する

関連した質問