views:

269

answers:

1

I have an NSTableView with two columns and a data source that fills it with data. That part works.

How do I get data (NSString format) out of the NSTableView? I can get the selected row but NSTableView doesn't seem to have methods for accessing data.

Getting the data from my data source seems to return useless data (type id and NSLog() and NSRunAlertPanel() both claim that the data isn't of the type NSString.

What's the easiest way to get data from an NSTableView?

Update:

Turns out the reason getting data from my data source fails has to do with the returning NSString being part of an NSArray in the tableView() method.

My input file contains lines which I split up for the columns in my table view. The NSTableView gets the correct data but when I call the tableView() method myself I run into a EXC_BAD_ACCESS error which I assume has to do with memory management.

Another update:

It wasn't memory management. It was my application's inability to identify the NSTableColumn it wanted. I wrote a second method to access data manually which uses an int for the column rather than a NSTableColumn and that worked.

+5  A: 

It's a violation of the MVC paradigm to be using your table (a View) as a place to store your data (Model). You should be able to get the value from your data source the same way the table view does; what does your code for tableView:objectValueForTableColumn:row: do to get the data?

If your table is editable, you will get calls from the table view to tableView:setObjectValue:forTableColumn:row: to allow you to modify your data source appropriately.

Carl Norum
My data source reads the data from a file. But one of the arguments it takes for giving me data is a NSTableColumn? Which one? Also, the data it gives me appears to be something other than NSString but I don't know why.
Andrew J. Brehm
The column has a name and a tag you can check on. The table shouldn't be giving you any data at all...
Carl Norum
You are all overlooking my first sentence. I got the NSTableView to display data. My problem was getting the datasource to give me the same data it gave to the NSTableView. When I tried it using the tableView method it didn't work and I assumed there would be an easier method. There wasn't.
Andrew J. Brehm
Worked by using the right NSTableColumn pointer.
Andrew J. Brehm
It sounds as if your solution is likely a bandaid to a bigger design problem. I strongly recommend posting some relevant code and a few words on your overall design and goal. As an eight-year Cocoa veteran, I can tell you something smells very wrong based on the nature of your questions and the solution you found. Not a sleight, just a "woah, that doesn't sound right." :-)
Joshua Nozzi
Why? I am getting the data from my data source object. How can there be a design problem in a program with two classes (not counting a separate class that acts solely as a delegate for the main window)? The problem was that I didn't realise that I needed to get the NSTableColumn from the control in order to get the same data as the control did. I assume NSTableView uses NSTableColumns rather than ints for columns because the columns can be rearranged.
Andrew J. Brehm