Using a UITableview to create a checklist. Checklist is working but would like to create a button to clear all checkmarks so the checklist can be used again. The following is the code that is working to select an item and then put a checkmark next to it. Now I want to create a button to remove all checked items and start over. Have tried several things but can’t figure out how to implement the button. Thanks for your help.
override func tableView(_ tableView: UITableView, cellForRowAt indexPath: IndexPath) → UITableViewCell
{
let cell = tableView.dequeueReusableCell(withIdentifier: “cell_todo”, for: indexPath)
if indexPath.row < todoItems.count
{
let item = todoItems[indexPath.row]
cell.textLabel?.text = item.title
let accessory: UITableViewCell.AccessoryType = item.done ? .checkmark : .none
cell.accessoryType = accessory
}
return cell
}
override func tableView(_ tableView: UITableView, didSelectRowAt indexPath: IndexPath)
{
tableView.deselectRow(at: indexPath, animated: true)
if indexPath.row < todoItems.count
{
let item = todoItems[indexPath.row]
item.done = !item.done
tableView.reloadRows(at: [indexPath], with: .automatic)
}
Welcome to the community @TravelingCoder !!
In your function call for the button put this code. You need it iterate through each todo item and set your done value equal to false. And then reload the tableView
for item in todoItems {
Item.done = false
}
Thanks, it is just about working. I ran the code and reloaded the tableview (see below) but it doesn’t remove the checkmark from the screen until I click on another function or click the bottom button. Not sure how else to refresh the screen.
for item in todoItems {
item.done = false
}
tableView.reloadData()
What does your class or struct look like that defines the todoItems?
As an exercise I set up some code to replicate what you appear to be doing and populated the table with 100 dummy items which all have the todoItem.done set to false initially.
In order to make it easier to have a button visible I embedded the ViewController in a NavigationController and programatically created a button on the trailing side of the NavigationBar so that it looks like this:
If I go through and set the checkmark on a bunch of items like this:
And then hit the Reset button, they are all removed. Is that what you are trying to get to happen?
For clarity, this is the relevant code.
This is the code for the Model (ToDo.swift)
import Foundation
class ToDo {
var title: String
var done: Bool
init(title: String, done: Bool) {
self.title = title
self.done = done
}
static func addDummyData() -> [ToDo] {
var dummyData: [ToDo] = []
for i in 0..<100 {
let newItem = ToDo(title: "Item \(i)", done: false)
dummyData.append(newItem)
}
return dummyData
}
}
and this is the code in the ViewController:
import UIKit
class ViewController: UIViewController, UITableViewDelegate, UITableViewDataSource {
@IBOutlet var tableView: UITableView!
var todoItems: [ToDo] = []
override func viewDidLoad() {
super.viewDidLoad()
// Do any additional setup after loading the view.
let resetButton = UIBarButtonItem(title: "Reset", style: .plain, target: self, action: #selector(resetCheckmarks))
navigationItem.rightBarButtonItems = [resetButton]
title = "ToDo List"
navigationController?.navigationBar.prefersLargeTitles = true
tableView.delegate = self
tableView.dataSource = self
todoItems = ToDo.addDummyData()
}
func tableView(_ tableView: UITableView, numberOfRowsInSection section: Int) -> Int {
return todoItems.count
}
func tableView(_ tableView: UITableView, cellForRowAt indexPath: IndexPath) -> UITableViewCell {
let cell = tableView.dequeueReusableCell(withIdentifier: "Cell", for: indexPath)
let item = todoItems[indexPath.row]
cell.textLabel!.text = item.title
let accessory: UITableViewCell.AccessoryType = item.done ? .checkmark : .none
cell.accessoryType = accessory
return cell
}
func tableView(_ tableView: UITableView, didSelectRowAt indexPath: IndexPath) {
tableView.deselectRow(at: indexPath, animated: true)
let item = todoItems[indexPath.row]
item.done = !item.done
tableView.reloadRows(at: [indexPath], with: .automatic)
}
@objc func resetCheckmarks() {
for item in todoItems {
item.done = false
}
tableView.reloadData()
}
}
Thanks for your help. The problem seems to be with my version of Xcode. Tried it on another computer and it worked fine.
Thanks again for the quick help!!