DEV Community

Maegan Wilson
Maegan Wilson

Posted on

Day 002: Making a TODO list

Click here for the code! // Link goes to GitHub.

What I wanted to accomplish

  1. Make the UITextField work
  2. Make the button work
  3. Populate the table view

Things I accomplished:

  1. Ability to type in a task (number 1 above)
  2. Add task to the todo list (number 2 above)
  3. Dismiss the keyboard after adding a todo with the button.(number 1 & 2)
    1. The keyboard does not dismiss if using the Done key
  4. Populate the table view (number 3 above)

Things I learned

Creating a separate class for a todo list made things a little bit more difficult.

struct Todo {
    var task: String
    var complete:Bool = false
}

class TodoList {
    private var todoList = [Todo]()

    func addTodo(todo: String) {
        let newTodo = Todo(task: todo, complete: false)
        todoList.append(newTodo)
    }

    func returnList() -> Array<Todo> {
        return todoList
    }

    func count() -> Int {
        return todoList.count
    }

    func getTodo(index: Int) -> Todo {
        return todoList[index]
    }
}
Enter fullscreen mode Exit fullscreen mode

For example, if I needed to grab a Todo at a specific index, I had to write my own function for it. This is also the same for getting the number of todos in the todo list.

I used the class and structs to help with organization and to also keep my data out of my ViewController.swift file.

Linking a table to a view controller

Something I had trouble on was linking the table view to the view controller. I found some online tutorials to help me, but none explained how to connect the two. I will explain it right now.

While holding option, click and drag from the table view, to ViewController.swift inside the ViewController class to make an IBOutlet.
Here is what my line of code looks like:

@IBOutlet weak var todoTable: UITableView!
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!

Top comments (0)