Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

Extract a range from NSArray

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

like image 330
joec Avatar asked Oct 04 '10 16:10

joec


2 Answers

 static const NSUInteger ItemsPerView = 20;
 NSUInteger startIndex = viewIndex * ItemsPerView;
 NSUInteger count = MIN( completeArray.count - startIndex, ItemsPerView );
 NSArray *itemsForView = [completeArray subarrayWithRange: NSMakeRange( startIndex, count )];
like image 174
Sven Avatar answered Oct 20 '22 17:10

Sven


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.

like image 27
Joshua Nozzi Avatar answered Oct 20 '22 17:10

Joshua Nozzi