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

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

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

TableView(UITableView)とは、リスト形式で表示するコントロールで、ほとんどのアプリに使用されています。画面を「行」に分けて管理し、一般的には各行をタップした際に詳細画面に移動します。

iOS

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

Xcode

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

Swift

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

iPhone

iPhoneとは、アップル社が開発・販売しているスマートフォンです。 同社のデジタルオーディオプレーヤーiPodの機能、電話機能、インターネットやメールなどのWeb通信機能の3つをドッキングした機器です。

Q&A

解決済

1回答

1368閲覧

tableviewのセルに表示されたチェックマークの一括リセットができるボタンの実装。

GO96805224

総合スコア13

TableView

TableView(UITableView)とは、リスト形式で表示するコントロールで、ほとんどのアプリに使用されています。画面を「行」に分けて管理し、一般的には各行をタップした際に詳細画面に移動します。

iOS

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

Xcode

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

Swift

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

iPhone

iPhoneとは、アップル社が開発・販売しているスマートフォンです。 同社のデジタルオーディオプレーヤーiPodの機能、電話機能、インターネットやメールなどのWeb通信機能の3つをドッキングした機器です。

0グッド

0クリップ

投稿2020/08/14 23:03

編集2020/08/15 09:35

前提・実現したいこと

tableviewのセルに表示されたチェックマークの一括リセットができるボタンの実装。

ここに質問の内容を詳しく書いてください。
tableviewのセルに表示されたチェックマークの一括リセットができるボタンをnavigationcontroller内のresetボタンに実装実装したいのですが上手くいきません。

tableviewの実装は下記サイトを参考に実装しました。
https://qiita.com/TheAtlasEngine/items/d95ebbd4b82d47ee844c

swift

1import UIKit 2 3final class ViewController: UIViewController, UIImagePickerControllerDelegate, UINavigationControllerDelegate { 4 5 @IBOutlet private weak var tableView: UITableView! 6 7 private var viewModel: ViewModel! 8 private let reuseCellId = "TodoCell" 9 10 @IBOutlet weak var button: UIButton! 11 12 @IBOutlet weak var camera: UIButton! 13 14 15 @IBAction func reset(_ sender: Any) { 16 func tableView(_ tableView: UITableView, numberOfRowsInSection section: Int) -> Int { 17 viewModel.currentTodos.count 18 } 19 20 func tableView(_ tableView: UITableView, cellForRowAt indexPath: IndexPath) -> UITableViewCell { 21 let cell = UITableViewCell(style: .default, reuseIdentifier: reuseCellId) 22 23 cell.accessoryType = .none 24 25 return cell 26 } 27 } 28 29 30 override func viewDidLoad() { 31 super.viewDidLoad() 32 33 34 viewModel = ViewModel() 35 36 tableView.delegate = self 37 tableView.dataSource = self 38 39 } 40 41 42 @IBAction func startUiImagePickerController(_ sender: Any) { 43 let picker = UIImagePickerController() 44 picker.sourceType = .camera 45 picker.delegate = self 46 // UIImagePickerController カメラを起動する 47 present(picker, animated: true, completion: nil) 48 } 49 50 51 func imagePickerController(_ picker: UIImagePickerController, didFinishPickingMediaWithInfo info: [UIImagePickerController.InfoKey : Any]) { 52 let image = info[.originalImage] as! UIImage 53 54 UIImageWriteToSavedPhotosAlbum(image, nil, nil, nil) 55 56 self.dismiss(animated: true, completion: nil) 57 } 58 59 @IBAction private func onAddButtonTapped(_ sender: Any) { 60 UIAlertController 61 .makeAlertWithTextField(title: "チェックを追加", message: nil, textFieldConfig: { $0.placeholder = "Title" }) 62 .addDefaultActionWithText(title: "追加") { [weak self] text in 63 guard !text.isEmpty else { return } 64 self?.add(todo: Todo(title: text, isDone: false)) 65 } 66 .addCancelAction() 67 .present(from: self) 68 } 69 70} 71private extension ViewController { 72 73 func todo(forRowAt indexPath: IndexPath) -> Todo { 74 viewModel.currentTodos[indexPath.row] 75 } 76 77 func add(todo: Todo) { 78 viewModel.add(todo: todo) 79 tableView.reloadData() 80 } 81 82 func updateTodo(forRowAt indexPath: IndexPath, to newTodo: Todo) { 83 viewModel.updateTodo(for: todo(forRowAt: indexPath).id, to: newTodo) 84 tableView.reloadData() 85 } 86 87 func deleteTodo(forRowAt indexPath: IndexPath) { 88 viewModel.deleteTodo(for: todo(forRowAt: indexPath).id) 89 tableView.reloadData() 90 } 91} 92extension ViewController: UITableViewDelegate { 93 94 func tableView(_ tableView: UITableView, didSelectRowAt indexPath: IndexPath) { 95 96 let selectedTodo = todo(forRowAt: indexPath) 97 let newTodo = Todo( 98 id: selectedTodo.id, 99 title: selectedTodo.title, 100 isDone: !selectedTodo.isDone 101 ) 102 103 updateTodo(forRowAt: indexPath, to: newTodo) 104 } 105 106 //削除ボタン 107 func tableView(_ tableView: UITableView, leadingSwipeActionsConfigurationForRowAt indexPath: IndexPath) -> UISwipeActionsConfiguration? { 108 109 let deleteAction = UIContextualAction( 110 style: .destructive, 111 title: "項目を削除" 112 ) { [weak self] (_, _, _) in 113 self?.deleteTodo(forRowAt: indexPath) 114 } 115 116 return .init(actions: [deleteAction]) 117 } 118 119 //編集ボタン 120 func tableView(_ tableView: UITableView, trailingSwipeActionsConfigurationForRowAt indexPath: IndexPath) -> UISwipeActionsConfiguration? { 121 122 let oldTodo = todo(forRowAt: indexPath) 123 124 let editAction = UIContextualAction(style: .destructive, title: "編集") { [weak self] (_, _, _) in 125 guard let self = self else { return } 126 127 UIAlertController 128 .makeAlertWithTextField(title: "項目を編集", message: nil, textFieldConfig: { $0.placeholder = "New Title" }) 129 .addDefaultActionWithText(title: "OK") { [weak self] newTitle in 130 guard !newTitle.isEmpty else { return } 131 132 let newTodo = Todo(id: oldTodo.id, title: newTitle, isDone: oldTodo.isDone) 133 self?.updateTodo(forRowAt: indexPath, to: newTodo) 134 } 135 .addCancelAction() 136 .present(from: self) 137 } 138 editAction.backgroundColor = .systemTeal 139 140 return .init(actions: [editAction]) 141 } 142} 143 144//cellに文字を表示 145extension ViewController: UITableViewDataSource { 146 147 func tableView(_ tableView: UITableView, numberOfRowsInSection section: Int) -> Int { 148 viewModel.currentTodos.count 149 } 150 151 func tableView(_ tableView: UITableView, cellForRowAt indexPath: IndexPath) -> UITableViewCell { 152 let cell = UITableViewCell(style: .default, reuseIdentifier: reuseCellId) 153 154 cell.textLabel?.text = todo(forRowAt: indexPath).title 155 156 cell.accessoryType = todo(forRowAt: indexPath).isDone ? .checkmark : .none 157 158// if cell.accessoryView == nil { 159// cell.accessoryView = UISwitch() 160// } 161 //cell中の文字色を変更する 162 cell.textLabel?.textColor = .white 163 //cell色を変更する 164 cell.backgroundColor = .darkGray 165 //cell.accessoryType = .none 166 return cell 167 } 168} 169 170 171 172

試したこと

storyboard中のresetボタンとViewcontroller .swifを@IBActionで接続し、
cell.accessoryType = .none でセルに表示されているチェックマークを全て消せるのではと思い、試しましたが、上手くいきませんでした。

誠に心苦しいのですが、どなたか改善方法をご教授いただけないでしょうか?
何卒よろしくお願い致します。

ここに問題に対して試したことを記載してください。

swift

1@IBAction func reset(_ sender: Any) { 2 func tableView(_ tableView: UITableView, numberOfRowsInSection section: Int) -> Int { 3 viewModel.currentTodos.count 4 } 5 6 func tableView(_ tableView: UITableView, cellForRowAt indexPath: IndexPath) -> UITableViewCell { 7 let cell = UITableViewCell(style: .default, reuseIdentifier: reuseCellId) 8 9 cell.accessoryType = .none 10 11 return cell 12 } 13 }

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

ここにより詳細な情報を記載してください。

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

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

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

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

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

GO96805224

2020/08/15 09:36

ご指摘ありがとうございます。 ソースコードをマークアップ機能を使用し、提示し直しましたので、 お時間があればご確認のほどよろしくお願い致します。
guest

回答1

0

ベストアンサー

そもそも、メソッド(関数)の実装方法が間違っているようです。

Swift

1 @IBAction func reset(_ sender: Any) { 2 func tableView(_ tableView: UITableView, numberOfRowsInSection section: Int) -> Int { 3 viewModel.currentTodos.count 4 } 5 6 func tableView(_ tableView: UITableView, cellForRowAt indexPath: IndexPath) -> UITableViewCell { 7 let cell = UITableViewCell(style: .default, reuseIdentifier: reuseCellId) 8 9 cell.accessoryType = .none 10 11 return cell 12 } 13 }

とありますが、メソッド内でメソッドを定義しているだけなので、実質何も行いません(文法的には間違いではないかとおもいますが、今回の目的では意味を成していません)。

「リセット」が何を持ってリセットするのか次第ですが、たとえば記録している ToDo のデータの isDone をリセットするのであれば、そのように記述し、適切なタイミングで TableView をリロードすればいいかと思われます。

##追記

リセットについては、例えば以下のようなメソッドを追加し、ボタンを押した時にこのメソッドを呼べばいいかと思います(処理方法は他のメソッドの内容に揃えました)。

Swift

1private extension ViewController { 2 // 前略 3 func resetAllDone() { 4 for oldTodo in viewModel.currentTodos { 5 let newTodo = Todo(id: oldTodo.id, title: oldTodo.title, isDone: false) 6 7 viewModel.updateTodo(for: oldTodo.id, to: newTodo) 8 } 9 tableView.reloadData() 10 } 11}

投稿2020/08/15 22:56

編集2020/08/16 03:21
TsukubaDepot

総合スコア5086

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

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

GO96805224

2020/08/16 01:31

ご回答ありがとうございます。 ご指摘どおりisDoneをリセットしようと思うのですが、 どのようにコーディングすべきかも併せて教えていただけないでしょうか? 誠に勝手ながらご教授お願い致します。
TsukubaDepot

2020/08/16 03:21

回答本文に追記しましたのでご確認ください。
GO96805224

2020/08/18 00:13

ご指摘どおり上記コードを実施したら、思うように動いてくれました! 本当にありがとうございました!
guest

あなたの回答

tips

太字

斜体

打ち消し線

見出し

引用テキストの挿入

コードの挿入

リンクの挿入

リストの挿入

番号リストの挿入

表の挿入

水平線の挿入

プレビュー

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

ただいまの回答率
85.48%

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

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

質問する

関連した質問