My Solutions for the Challenge

Didn’t see any solutions for the challenge, so I figured I would give my solution. Unfortunately at this point I wasn’t an overachiever, and just implemented the adding of tableview cells. I’ll try doing that sometime soon and update my solution accordingly.

Here’s my MainWindowControler.swift file:

[code]
import Cocoa

class MainWindowController: NSWindowController, NSTableViewDataSource, NSTableViewDelegate {

@IBOutlet weak var tableView: NSTableView!
@IBOutlet weak var taskField: NSTextField!

var items = [String]()

override var windowNibName: String {
    return "MainWindowController"
}

override func windowDidLoad() {
    super.windowDidLoad()
}

// MARK: - NSTableViewDataSource

func numberOfRowsInTableView(tableView: NSTableView) -> Int {
    return items.count
}

func tableView(tableView: NSTableView, objectValueForTableColumn tableColumn: NSTableColumn?, row: Int) -> AnyObject? {
    let item = items[row]
    return item
}

// MARK: - Action Methods

@IBAction func addTask(sender: NSButton) {
    let newTask = taskField.stringValue
    if newTask.isEmpty {
        print("Not sure what you're trying to do, \(newTask) is empty")
    } else {
        items.insert(newTask, atIndex: items.count)
    }
    tableView.reloadData()
}

}[/code]

This solution is updated for 3.0.1 Swift. Also added to clear the text field after the add button is pushed. And to auto scroll the table so the last value entered is visible.

//
// MainWindowController.swift
// ToDo
//
// Created by Philip Blen on 12/3/16.
// Copyright © 2016 Philip Blen. All rights reserved.
//

import Cocoa

class MainWindowController: NSWindowController, NSWindowDelegate,NSTableViewDataSource, NSTableViewDelegate {

@IBOutlet weak var inputTextField: NSTextField!
@IBOutlet weak var addButton: NSButton!
@IBOutlet weak var toDoTableView: NSTableView!

  var items = [String]()

override var windowNibName: String {
    return "MainWindowController" }

override func windowDidLoad() {
    super.windowDidLoad()
}
    // Implement this method to handle any initialization after your window controller's window has been loaded from its nib file.

func numberOfRows(in toDoTableView: NSTableView) -> Int {
    print ("numberOfRows \(items.count)")
    return items.count
    }

func tableView(_ toDoTableView: NSTableView, objectValueFor tableColumn: NSTableColumn?, row: Int) -> Any? {
    print (row)
    let item = items[row]
    print(item)
    return item
}

@IBAction func addButton ( sender: NSButton) {
    // Get typed-in text as a string
   // put text into items
    let newItem = inputTextField.stringValue
    inputTextField.stringValue = ""
   // print (items.count)
    items.insert(newItem, at: items.count)
    print ( items)
    toDoTableView.reloadData()
    toDoTableView.scrollRowToVisible(items.endIndex - 1)
    
}

}