View-Based NSTableView in Swift - How to
Asked Answered
H

4

7

I have a NSTableView whose cells are view-based.

DataSource & Delegate are connected, but I'm not able to display the cell's textField string value.

This is the code in Objective-C, working:

- (NSInteger)numberOfRowsInTableView:(NSTableView *)tableView {

return 10;

}

- (NSView *)tableView:(NSTableView *)tableView viewForTableColumn:(NSTableColumn *)tableColumn row:(NSInteger)row {

        NSTableCellView *cell = [tableView makeViewWithIdentifier:@"List" owner:self];
        [cella.textField setStringValue:"Hey, this is a cell"];

        return cell;
}

And here is my code in Swift, not working :

func numberOfRowsInTableView(aTableView: NSTableView!) -> Int
{
    return 10 //Casual number
}
func tableView(tableView: NSTableView!, cellForRowAtIndexPath indexPath: NSIndexPath!) -> NSTableCellView! {
    var cell = tableView.makeViewWithIdentifier("List", owner: self) as NSTableCellView!
    // setup cell without force unwrapping it
    cell.textField.stringValue = "Hey, this is a cell"
    println("Method called") //Never printed
    return cell
}

This is the result: (table on right side of image)

Note that the comment //setup cell without force unwrapping it makes no sense, I forgot to delete it.

Code + Result TableVie settings Delegate and DataSource connected

What I am missing ?

Edit: I tried even the following with no success:

func numberOfRowsInTableView(aTableView: NSTableView!) -> Int
{
    return 10
}
func tableView(tableView: NSTableView!, objectValueForTableColumn tableColumn: NSTableColumn!, row: Int) -> AnyObject
{
    var cell = tableView.makeViewWithIdentifier("List", owner: self) as NSTableCellView
    cell.textField.stringValue = "Hey this is a cell"
    return cell;
}

Thank you all. Alberto

Heroic answered 21/6, 2014 at 22:20 Comment(1)
in Objective-C code you have tableView:viewForTableColumn:row:, while in Swift you have tableView(tableView, cellForRowAtIndexPath). Surely there's tableView:viewForTableColumn:row: Swift equivalent?Barrio
H
12

After hours of search, I discovered this method that works !

func tableView(tableView: NSTableView, viewForTableColumn: NSTableColumn, row: Int) -> NSView
{
    var cell = tableView.makeViewWithIdentifier("List", owner: self) as NSTableCellView
    cell.textField.stringValue = "Hey, this is a cell"
    return cell;
}
Heroic answered 22/6, 2014 at 9:23 Comment(0)
F
3

I see that you found your answer your self but from what I can see your clue was in the Return Value of Objective -C delegate.

- (NSView *)tableView:...

The return value is a NSView.

But you should look at the Swift/Objective -c documentaion.

From the Docs:

Providing Views for Rows and Columns tableView:viewForTableColumn:row:

Asks the delegate for a view to display the specified row and column.

Declaration
SWIFT
@optional func tableView(_ tableView: NSTableView!,
      viewForTableColumn tableColumn: NSTableColumn!,
                     row row: Int) -> NSView!
OBJECTIVE-C
- (NSView *)tableView:(NSTableView *)tableView
   viewForTableColumn:(NSTableColumn *)tableColumn
                  row:(NSInteger)row

Note the -> NSView! in the swift code also.

The new docs allow you to see the code for Swift and Objective -c side by side or one or the other. You can use a selection tab at the top of the documentation to choose.

It also looks like your code should include the "!" for optionals

Feudal answered 22/6, 2014 at 11:17 Comment(0)
Z
2

If you change the content mode of the table view from view based to cell based the method is called.

enter image description here

Zoom answered 28/12, 2014 at 8:46 Comment(0)
S
0

This beat me up for an hour. Works with Swift 2.2, probably won't work for earlier or later versions:

 let cell = tableView.makeViewWithIdentifier(myid!, owner: nil)  // You can't cast this with as! like they want you to

    if let mycell = cell as? NSTableCellView {
        mycell.textField?.stringValue = text
        return mycell
    }
Sawmill answered 6/7, 2016 at 0:29 Comment(0)

© 2022 - 2024 — McMap. All rights reserved.