green_knight
green_knight

Reputation: 1385

View-based table bindings in Swift

I'm trying to set up a view-based table in Swift using bindings. All of the examples I've seen use a datasource/delegate setup.

I have an array of Flag objects which has two properties - flagName: String and flagImage: NSImage. I have an NSArrayController managing this array.

If I set up a cell-based table, and bind one column to arrangedObjects.flagImage and the other to arrangedObjects.flagName, I get a table displaying images and names, and I can use the array controller's add and remove methods, so there are no problems with my datasource or my array controller.

I have been following the instructions in Apple's TableView Programming Guide to bind my view-based table to my array controller:

(IBs autocomplete is not happy with the paths for objectValue.flagName respectively flagImage; it doesn't feel that there should be any completion whatsoever and says it can't resolve the path, so it looks as if the problem is with the tableView's content.)

If I do this, my table has a number of rows that corresponds to the number of elements that my array controller is managing at that moment (I have two simple setups, one object vs. 50 objects, so it's clear that something is bound). What I don't get is a display; and selecting a table row does not seem to send back a message to my flagController.

What am I missing? Has anyone been able to make this work? I've had no problems with other bindings in Swift so far, but I'm starting to think that the sudden reappearance of datasource examples is not unrelated to this.

Upvotes: 3

Views: 2465

Answers (1)

Ken Thomases
Ken Thomases

Reputation: 90531

It sounds like you've failed to implement the delegate method -tableView:viewForTableColumn:row:. That's a common cause of blank tables.

You don't actually have to implement that if you make sure the identifier of the table column and the identifier of the table cell view are the same in IB.

Otherwise, the method can just do this:

- (NSView*) tableView:(NSTableView*)tableView viewForTableColumn:(NSTableColumn*)tableColumn row:(NSInteger)row
{
    return [tableView makeViewWithIdentifier:@"TheIdentifierOfYourTableCellView" owner:self];
}

(It could also do more, if desired.)

Upvotes: 2

Related Questions