DEV Community

Maegan Wilson
Maegan Wilson

Posted on

4 3

Day 005: Making a TODO list

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
    }
Enter fullscreen mode Exit fullscreen mode

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)
            }
        }
Enter fullscreen mode Exit fullscreen mode

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!

AWS Security LIVE!

Join us for AWS Security LIVE!

Discover the future of cloud security. Tune in live for trends, tips, and solutions from AWS and AWS Partners.

Learn More

Top comments (0)

AWS GenAI LIVE image

Real challenges. Real solutions. Real talk.

From technical discussions to philosophical debates, AWS and AWS Partners examine the impact and evolution of gen AI.

Learn more

👋 Kindness is contagious

Please leave a ❤️ or a friendly comment on this post if you found it helpful!

Okay