Parse Data into NSTableView

前端 未结 2 422
旧巷少年郎
旧巷少年郎 2021-02-04 19:01

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

相关标签:
2条回答
  • 2021-02-04 19:41

    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
    }
    
    0 讨论(0)
  • 2021-02-04 20:00

    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

    0 讨论(0)
提交回复
热议问题