hutch34
hutch34

Reputation: 35

Why does this code in Swift produce an empty row on NSTableView?

I'm (obviously) new to Swift, and I'm new to Cocoa programming in general. When I build this code and run it, it automatically inserts an empty row on the NSTableView for peopleList[0], so when I go to add a new person, they show up in the second row, and the first remains empty. I'm at a loss as to why this is. I know I can probably remove the blank row programmatically before inserting the first row of data, but I'd like to know the root cause of this if possible.

Here is my code:

import Cocoa

class AppDelegate: NSObject, NSApplicationDelegate, NSTableViewDataSource,     NSTableViewDelegate {

    @IBOutlet var window: NSWindow

    // Table View Outlet
    @IBOutlet var tableViewData : NSTableView

    // Text Field Cell Outlets
    @IBOutlet var firstNameTextField : NSTextField
    @IBOutlet var lastNameTextField : NSTextField
    @IBOutlet var ageTextField : NSTextField
    @IBOutlet var netWorthTextField : NSTextField


    // declare an array of multiple dictionaries
    var peopleList = [Dictionary <String, String>()]


    func applicationDidFinishLaunching(aNotification: NSNotification?) {
        // Insert code here to initialize your application

    }

    func applicationWillTerminate(aNotification: NSNotification?) {
        // Insert code here to tear down your application
    }

    func numberOfRowsInTableView(aTableView: NSTableView!) -> Int
    {
        let numberOfRows:Int = getDataArray().count
        return numberOfRows
    }

    func tableView(tableView: NSTableView!, objectValueForTableColumn tableColumn: NSTableColumn!, row: Int) -> AnyObject!
    {

        var newString = getDataArray().objectAtIndex(row).objectForKey(tableColumn.identifier)
        return newString;
    }


    func getDataArray() -> NSArray {
        return peopleList
    }

    // When button is hit, add NSTextField values to array and refresh the tableview
    @IBAction func addFieldDataToArray(sender : AnyObject) {
        peopleList.append(["FirstName": firstNameTextField.stringValue, "LastName": lastNameTextField.stringValue, "Age": ageTextField.stringValue, "NetWorth": netWorthTextField.stringValue])
        println(peopleList)
        tableViewData.reloadData()
    }

}

Upvotes: 1

Views: 1161

Answers (1)

Nate Cook
Nate Cook

Reputation: 93276

This is happening because of the way you're initializing and appending to peopleList. Upon initialization, peopleList is an array of Dictionary<String, String> and contains a single empty dictionary:

> var peopleList = [Dictionary <String, String>()]
[[String : String]] = 1 value {
  [0] = {}
}

After adding a person, it looks like this -- you've appended a new person's information, but that empty initial dictionary is still there.

[[String : String]] = 2 values {
  [0] = {}
  [1] = {
    [0] = {
      key = "NetWorth"
      value = "About 10 bucks"
    }
    [1] = {
      key = "LastName"
      value = "Schmoe"
    }
    [2] = {
      key = "Age"
      value = "37"
    }
    [3] = {
      key = "FirstName"
      value = "Joe"
    }
  }
}

I'd recommend starting out with peopleList completely empty -- no empty row to worry about:

> var peopleList: [Dictionary<String, String>] = []
[[String : String]] = 0 values

Upvotes: 2

Related Questions