简体   繁体   中英

Parse Data into NSTableView

Hello fellow programmers,

First off, I am new to OSX/iOS programming and am trying to learn swift. I am fairly competent in java and wanted to teach myself something new. I am aware that the swift programming language is in it's early stages.

I have a table using the parse.com data browser. I can get the elements from that table and store them in an array. Normally, in java, I would then simply iterate over those elements and populate a JTable. Now, my question to you is...How would I go about doing this? I have tried to read the apple api and I felt my IQ dropping. I can understand the getNumberOfRows function/method however the other functions mean nothing to me. I have an NSTable in my xib file and have west the delegate and datasource to the app delegate file. How do I set the identifier so that I can start setting information.

I understand that I am not really helping but any advice is greatly appreciated.

Thank you.

Yes, tableviews with Cocoa are... complicated.

The absolute basics for getting content in the tableview is to set its datasource, which has to adopt NSTableViewDataSource protocol. Then you have to differentiate between a view based and a cell based tableview. In the view based you can use a view you designed in Interface Builder, the cell based are older and simpler. You understand the numberOfRowsInTableView function, so I proceed with the more complicated functions.

Cell based TableVies

For cell based tableViews, the second essential function## Heading ## is

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

When you just got it, it's pretty easy. As first parameter you get the tableView (only interesting if you use the same dataSource for multiple tableViews). The second specifies the tableColumn. You can identify a column by using its identifier. You set this identifier in the InterfaceBuilder. Click on your tableview until a column is selected. Then set in the sidebar the Restoration ID. As last parameter you get your row. You return an object of type AnyObject. I normally return a string, I don't know whether NSNumber is also valid. A simple example implementation is the following:

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

    var columnIdentifier = tableColumn.identifier
    if columnIdentifier == "number" {
        result = "\(row+1)"
    }
    if columnIdentifier == "name" {
        result = model.persons[row].name
    }
    if columnIdentifier == "lastName" {
        result = model.persons[row].lastName
    }
    return result
}

When you want to set values, use

func tableView(tableView: NSTableView!, setObjectValue object: AnyObject!, forTableColumn tableColumn: NSTableColumn!, row: Int)

Object represents the new value you should transfer to your data model.

View based TableViews

In view based tableViews, the things lays different.

Here you have to design a custom view in a separate nib file. Then, you register the nib in your initializer.

let nib = NSNib(nibNamed: "Example", bundle: NSBundle.mainBundle())
        self.tableView.registerNib(nib, forIdentifier: "Example")

This allows you to create instances of your view using makeViewWithIdentifier:owner: Then you can configure your view (I like to subclasses NSView for this) and pass it back as result of

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

Example implementation:

func tableView(tableView: NSTableView!, viewForTableColumn tableColumn: NSTableColumn!, row: Int) -> NSView!{
    let view = tableView.makeViewWithIdentifier("Example", owner: self) as MyCustomView
    view.field1.stringValue = model.persons[row].name
    view.field2.stringValue = model.persons[row].lastName
    view.field3.stringValue = "\(row+1)"
    return view
}

The way we do it in iOS is, In the class that is the TableView's Delegate and Datasource we implement

func tableView(tableView: UITableView!, cellForRowAtIndexPath indexPath: NSIndexPath!) -> UITableViewCell!{


    var ident = "cellIdentifier"
    var cell = self.tableView.dequeueReusableCellWithIdentifier(ident) as UITableViewCell
    if (cell == nil) {
        cell = UITableViewCell(style: UITableViewCellStyle.Default, reuseIdentifier: ident)
    }
    cell.textLabel.text = myTitleArray[indexPath.row]
    return cell

}

To handle the user selecting a row:

func tableView(tableView: UITableView!, didSelectRowAtIndexPath indexPath: NSIndexPath!){

}

And to tell it how many rows there are:

func tableView(tableView: UITableView!, numberOfRowsInSection section: Int) -> Int{
    return myTitleArray.count
}

Where myTitleArray is your array of titles, obviously you can change that as you wish.

The way you find out what you should actually put as var ident = "cellIdentifier" is the is the identifier you gave the dynamic prototype cell in the storyboard

I agree sometimes the Apple Developer Class Reference can be confusing and often they have a Programming Guide which is way more helpful.

Try looking here for what I understand is what you want.

If your very interested in iOS programming check out these free Stanford lectures on iTunes U particularly 11, 12, and 13 talk about UITableView. They are in iOS 7 so nothing about Swift, but the only real difference in the implementation of UITableView is syntax between those languages

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM