Hello,
I am trying to build an app using tableView with two columns (one displaying strings, the other one a CheckBox for each string).
The strings are coming from a [AnyObject] variable.
I have try so many things to insert the result of [AnyObject] into the NsMutableDictionnary (which is used by NsTableView) but none of them works.
I have to build the dic manually with the right number of object (related to the result of [AnyObject]) to make it work but I would like the dic to build itself automatically.
Here is an exemple of the code, working if [AnyObject] is made with 4 elements. The TableView column names are "jobs" and "unload" :
import Cocoa
@NSApplicationMain
class AppDelegate: NSObject, NSApplicationDelegate,NSTableViewDataSource,NSTableViewDelegate {
@IBOutlet weak var window: NSWindow!
@IBOutlet weak var myTableView: NSTableView!
var dataArray:[NSMutableDictionary] = [["jobs": "", "unload": 0],["jobs": "", "unload": 0],["jobs": "", "unload": 0],["jobs": "", "unload": 0]];
func applicationDidFinishLaunching(aNotification: NSNotification) {
...
...
...
var ctlelems:[AnyObject] = grepOut!.componentsSeparatedByCharactersInSet(NSCharacterSet.newlineCharacterSet())
dataArray = [["jobs": "ctlelems[0]", "unload": 0],["jobs": "ctlelems[1]", "unload": 0],["jobs": "ctlelems[2]", "unload": 0],["jobs": "ctlelems[3]", "unload": 0]];
}
func applicationWillTerminate(aNotification: NSNotification) {
}
func numberOfRowsInTableView(aTableView: NSTableView) -> Int
{
return dataArray.count
}
func tableView(tableView: NSTableView, objectValueForTableColumn tableColumn: NSTableColumn?, row: Int) -> AnyObject?
{
let object = dataArray[row] as NSMutableDictionary
if ((tableColumn!.identifier) == "unload")
{
println(object)
return object[tableColumn!.identifier] as? Int!
}
else
{
return object[tableColumn!.identifier] as? String!
}
}
func tableView(tableView: NSTableView, setObjectValue object: AnyObject?, forTableColumn tableColumn: NSTableColumn?, row: Int)
{
dataArray[row].setObject(object!, forKey: (tableColumn?.identifier)!)
}
}
I would like to do the following,
var x: Int = 0
for lines in ctlelems {
dataArray = [["jobs": ctlelems[x], "unload": 0]];
x=x+1
}
but it is not working.
Most of the time I get "Found nill" error, or only the last element of [AnyObject] is displayed.
Could someone help me please?