GitHub Repo

Things I want to accomplish:

  • Marking completed todos with a checkmark
  • Sort completed tasks to the bottom of the list

Things I accomplished

  • Marking a completed todo with a checkmark
  • Sort completed tasks to the bottom of the list

Things I learned

If wanting to select rows make sure there is a UITableViewDelegate

Not having the UITableViewDelegate kept me from selecting and deselecting cells. It also took me a little bit to troubleshoot because at first glance I was doing what was necessary. After a couple of other stack overflow articles, I realized that I did not the UITableViewDelegate and did not assign it to self. Here is the final code necessary to make selecting work:

// UITableViewDelegate here
class ViewController: UIViewController, UITextFieldDelegate, UITableViewDelegate, UITableViewDataSource {
 
    @IBOutlet weak var todoTable: UITableView!

    var todos = TodoList()
    
    override func viewDidLoad() {
        super.viewDidLoad()
        // Do any additional setup after loading the view, typically from a nib.
        todoInput.delegate = self
        todoInput.returnKeyType = .done
        
        //table view setup
        todoTable.dataSource = self
        todoTable.delegate = self // <—— this line
    }

How to enumerate through items in Swift

To implement my sort, I needed to be able to go through each todo in my array and check whether or not it’s complete. I chose to use enumerated() because it will return the index of the todo and the actual todo itself.

for (i, todo) in todoList.enumerated(){
            if (todo.complete) {
                let element = todoList.remove(at: i)
                todoList.insert(element, at: todoList.count)
            }
        }

If you have any questions about what I did or how I implemented anything, let me know! If you have any suggestions or other comments, let me know as well!

This post is also available on DEV.