前提・実現したいこと
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/ツールのバージョンなど)
ここにより詳細な情報を記載してください。
回答1件
あなたの回答
tips
プレビュー