Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

NSTextField keeps repeating title

Tags:

macos

swift

cocoa

I created a new OSX application with Swift. Start with a view, dragged in a Table View into the view. Then I ctrl-clicked and dragged from my Table View to add AppDelegate as my dataSource and delegate. Finally, ctrl-clicked and dragged from AppDelegate to create an IBOutlet for myTableView to the table view.

Why is this happening? Using XCode Version 6.1 (6A1027).

Building the following results in this:Results

import Cocoa    
class AppDelegate: NSObject, NSApplicationDelegate, NSTableViewDataSource, NSTableViewDelegate {

    @IBOutlet weak var window: NSWindow!
    @IBOutlet weak var myTableView: NSTableView!

    func applicationDidFinishLaunching(aNotification: NSNotification?) {
    }

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

    func tableView(tableView: NSTableView!, objectValueForTableColumn tableColumn: NSTableColumn!, row rowIndex: Int) -> AnyObject!
    {
        var newString: (AnyObject?) = getDataArray().objectAtIndex(rowIndex).objectForKey(tableColumn.identifier)
        println(newString!)
        return newString!
    }

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

    func getDataArray () -> NSArray{
        var dataArray:[NSDictionary] = [["FirstName": "Debasis", "LastName": "Das"],
            ["FirstName": "Nishant", "LastName": "Singh"],
            ["FirstName": "John", "LastName": "Doe"],
            ["FirstName": "Jane", "LastName": "Doe"],
            ["FirstName": "Mary", "LastName": "Jane"]];
        //println(dataArray);
        return dataArray;
    }

}
like image 321
Steve Avatar asked Oct 20 '22 01:10

Steve


2 Answers

There was a Table View Cell also being added when I dragged in the Table View along with the text cell. I'm really not sure why, but this answer from 2 years ago helped me figure it out. NSTableView only displaying "Table View Cell"

like image 158
Steve Avatar answered Oct 23 '22 03:10

Steve


I noticed that "objectValueForTableColumn" only works if there's no other cell besides it. As Xcode6.1 automatically creates one, this method doesn't work anymore unless you remove the new cell.

To keep these cells and even add more, we should use "viewForTableColumn" instead: in IB, I named my table cell view "custom" then created a NSTableCellView to target it.

I also added another cell in IB, an ImageView, to illustrate my example.

let myQuotes: [String] = ["text field inside cell one", "number two", "here's the tird", "four: a fourth one", "five: and a last one"]

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

func tableView(tableView: NSTableView!, viewForTableColumn tableColumn: NSTableColumn!, row: Int) -> NSView! {

    var result: NSTableCellView = tableView.makeViewWithIdentifier("custom", owner: self) as NSTableCellView

    result.textField.stringValue = myQuotes[row]

    let myAvatar: NSImage = NSImage(byReferencingFile: "Pictures/avatar.png")

    result.imageView.image = myAvatar

    return result

}

I got this working after having jumped around this question and this Objective-C example for some time.

like image 37
Eric Aya Avatar answered Oct 23 '22 02:10

Eric Aya