views:

100

answers:

2

How would I get the total number of rows in an NSOutlineView?

+2  A: 

there is a method called numberOfRows, which returns a NSInteger. e.g. your NSOutlineView is called outlineView:

NSInteger *rows = [outlineView numberOfRows];

Documentation here:

http://developer.apple.com/mac/library/documentation/cocoa/Reference/ApplicationKit/Classes/NSOutlineView%5FClass/Reference/Reference.html

"Typically you should not ask the table view how many rows it has; instead you should interrogate the table view's data source."

There is an informal protocol, if you are really insistent on using the tableView, which is:

– numberOfRowsInTableView: (NSTableDataSource informal protocol)
Michael
+4  A: 

Does this include any subordinate rows in expanded rows? For example, if foo contains bar, baz, and qux, and is expanded, then bar, baz, and qux are visible rows as well. Conversely, if foo is not expanded, then bar, baz, and qux are not “rows in [the] NSOutlineView”—they exist only in your model.

Forget the outline view. It is irrelevant and confuses your question. You want to know how many of your model objects you have, so ask the controller that owns them.

People are saying “ask the data source” because that controller is the object that should be your view's data source. But forget the outline view, and forget its data source property. Talk to the controller directly. Ask it how many whatever-model-objects it has. If you want to know the deep version of the answer (how many things you have, including things in other things), ask it that. Either way, also implement a method in that controller to provide the answer.

Addendum: Remember the separation of powers in Cocoa. You have a model, which is the user's data in object form; views, which display the model to the user; and controllers, which own the model and the views. You say this isn't a drawing-related question; that makes it a question not for a view (“how many rows will you draw”), but a question for a controller (“how many <some-kind-of-thing>s do you own”). Therefore, you need to ask it of a controller.

Peter Hosey
True. This is what I've decided to do based on your answer. I have done a fetch request from the model object and then got the arrays count. Here … http://grab.by/1jKg. Is this what you were thinking? It works.
Joshua
It's not what I was thinking, but I'm not a Core Data guy. As far as I can tell, that's close enough. (BTW, don't ignore warnings. That warning is important. It also affects the line before it, which uses the correct formatter for the type you're using but the wrong formatter for the type you should be using.)
Peter Hosey