Day 005: Making a TODO list

Jay Wilson
2 min readFeb 2, 2019

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!

Sign up to discover human stories that deepen your understanding of the world.

Free

Distraction-free reading. No ads.

Organize your knowledge with lists and highlights.

Tell your story. Find your audience.

Membership

Read member-only stories

Support writers you read most

Earn money for your writing

Listen to audio narrations

Read offline with the Medium app

Jay Wilson
Jay Wilson

Written by Jay Wilson

I like to solve problems using Swift, make YouTube videos, and take photos. I also really enjoy a good cup of coffee.

No responses yet

Write a response