Stacking views
-
Hello,
I have a seemingly simple request, but unfortunately I can't find any
definitive Information on it. I need to have a view that stacks its
subviews in top of each other. Like a TableView. It gets its
represented Objects from an NSArrayController, and assigns it to some
View that it then adds as a subview. It also cares about the
selection, scrolling and so on. But the difficult point is that those
views can have different heights, dependent on their
representedObject. In some way the view stacking is similar to what's
done in Java's Swing for years. I know multiple approaches for it,
each with its caveats, and I'm not sure about which route to take:
NSTableViews/NSOutlineViews:
They don't use Subviews, they use Cells. As I want to use
CoreAnimation to move subviews around, this is a bit of a drawback.
However, Tables let me have rows with different heights, which is
important. I can use Cells which control views, but in this age of
CoreAnimation is this really the way to go?
NSCollectionView
This is almost perfect except that all subviews have the same size.
Overriding the layout mechanism to enable differently sized subviews
is less than trivial.
Using CALayoutManager
This will work for implementing the layout logic, but I will have to
do all the binding to the ArrayController with the represented Objects
myself, including selection, etc.
Doing it all from scratch
Is it really necessary to do it the hard way? Isn't there anything by
Apple or someone else that lets me stack views, bind them to an
ArrayController, update selections, etc, and animate everything nicely
with CoreAnimation?
I think I propably overlooked the obvious solution. Thanks in advance
for pointing me in the right direction!
Cheers,
Gernot. -
On 15 Feb '08, at 4:08 AM, Gernot wrote:
> NSCollectionView
> This is almost perfect except that all subviews have the same size.
> Overriding the layout mechanism to enable differently sized subviews
> is less than trivial.
Huh, I wasn't aware of that restriction. Is it documented?
NSCollectionView is pretty much a black box, with very little
customizability, so the only way to change the layout would be through
gross hackery.
> Using CALayoutManager
> This will work for implementing the layout logic, but I will have to
> do all the binding to the ArrayController with the represented Objects
> myself, including selection, etc.
This seems like the best approach. My guess is that the binding stuff
won't be too hard. And then you can publish your code and make other
developers happy :)
Do keep in mind, though, that NSViews are pretty heavyweight; putting
large numbers into a window will cause performance problems. (I don't
have an exact value for "large numbers", though. 100 or more?) That's
the reason NSCell exists. (And CALayer; layers seem to be a lot
lighter weight, though I found that putting shadows on them makes them
much slower.)
—Jens -
Am 15.02.2008 um 13:08 Uhr schrieb Gernot:
> I need to have a view that stacks its
> subviews in top of each other. Like a TableView. It gets its
> represented Objects from an NSArrayController, and assigns it to some
> View that it then adds as a subview. It also cares about the
> selection, scrolling and so on. But the difficult point is that those
> views can have different heights, dependent on their
> representedObject.
I did something like that.
It borrows some concepts from NSCollectionView. Right now you need to
assign an array of objects manually, but I guess creating a binding
should not be to difficult. It also does not use any animations.
Here is a small test app:
http://www.harmless.de/download/AMCollectionViewTest.zip
I'd probably need to add at least a bit of documentation before
publicly distributing the code though ...
Andreas -
On Feb 15, 2008, at 4:08 AM, Gernot wrote:
> Doing it all from scratch
> Is it really necessary to do it the hard way? Isn't there anything by
> Apple or someone else that lets me stack views, bind them to an
> ArrayController, update selections, etc, and animate everything nicely
> with CoreAnimation?
I didn't use bindings, but I wrote a "table view" with scrolling,
selection, variable row heights, row alternation etc that uses views,
and also animates new views and removing views (using
NSViewAnimation). The only problem is that animating more than just a
couple views gets reeeally slow unless you cache the image of the view
and make sure it's drawing that if it has to draw at all (I've yet to
use the code, so I didn't get this far). Otherwise, it worked fine.
--
Seth Willits



