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:
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;
}
}
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"
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.
If you love us? You can donate to us via Paypal or buy me a coffee so we can maintain and grow! Thank you!
Donate Us With