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()
}
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.