views:

41

answers:

2

I need to display 6 views and each view should display 20 items (UIButtons). I have one big NSArray which contains the items for all 6 views.

For example, view 1 should be items 0-19, view 2 should be items 20-39.

How would i extract the relevant range out of the array? Maybe using NSRange with a length of 20, but the start location would need to change for each view... without a switch statement ideally :)

Thanks

+2  A: 

Your answer is in your question. Just keep track of which NSRange belongs to which view and use it to look up the necessary objects in your container using NSArray's -subarrayWithRange: method.

Joshua Nozzi
All i have is a int representing the view, so is there a better way than `if (view == 0) { NSMakeRange(0,20); } else if (view == 1)...`?
joec
You'll need to provide more information. I'm not sure what you're asking here.
Joshua Nozzi
+2  A: 
 static const int kItemsPerView = 20;
 NSRange rangeForView = NSMakeRange( viewIndex * kItemsPerView, kItemsPerView );
 NSArray *itemsForView = [completeArray subarrayWithRange: rangeForView];
Sven