tags:

views:

21

answers:

1

I want to lay out several rows of NSViews. Each row will have start with say 5 NSViews with possibility to arbitrarily add or remove views between any of the starting 5, at beginning, or at the end.

1) How can I have these lay themselves out without me manually tracking the size of each and calculating position and placing each one? How can I say "NSView2 is to the right of NSView1 with a gap of 10 pixels"?

2) What data structure makes sense to hold these views and most simply maintain the positional relationships via Cocoa? Also I need to go back at the end and iterate from left to right through this list.

A: 

If all your views are of homogenous size, use NSCollectionView/Item. Otherwise, there's really no option for you but to maintain the grid (and tiling mechanism) yourself.

If your views "flow" or "wrap" (that is, inserting one view in row 2 pushes the last one in row 2 to the first position in row 3), you'd have to do your layout starting at least from the insertion position in the best case.

If your views don't wrap, then it's a lot easier. You only have to redo the layout for that row (and really, how many views do you expect to be in one row? if it's too many to lay out quickly, your design should really be reconsidered), starting from the insertion index.

If you want more detail, you'll need to specify exactly how your layout is expected to behave.

Joshua Nozzi