Monday, June 17, 2013

NSIndexSet countOfIndexesInRange example in Objective C (iOS).


NSIndexSet countOfIndexesInRange

Returns the number of indexes in the index set that are members of a given range.

- (NSUInteger)countOfIndexesInRange:(NSRange)indexRange

Parameters
indexRange
Index range being inquired about.

Return Value of [NSIndexSet countOfIndexesInRange]
Number of indexes in the index set that are members of indexRange.

NSIndexSet countOfIndexesInRange example.
NSArray *objectsToMove = [your_array objectsAtIndexes: rowIndexes];

// If any of the removed objects come before the row
// we want to decrement the row appropriately
row -= [rowIndexes countOfIndexesInRange: (NSRange){0, row}];

[your_array removeObjectsAtIndexes:rowIndexes];
[your_array replaceObjectsInRange: (NSRange){row, 0}
       withObjectsFromArray: objectsToMove];

Example of [NSIndexSet countOfIndexesInRange].
- (void) moveSelectedObjectsToRow:(NSUInteger)row {
>     NSIndexSet *indexes = [(NSArrayController*)self.representedObject
> selectionIndexes];
>     NSRange rangeAbove = NSMakeRange(0, row);   //  Range of all objects above
> row in table.
>     NSUInteger numAbove = [indexes countOfIndexesInRange:rangeAbove];  //  Num
> selected above row in table.
>     NSUInteger newRow = row - numAbove;  //  Where we move selection to.
>     [self.parentBase moveSublistObjectsAtIndexes:indexes toIndex:newRow];   //
> Replace this for Arrays.
> }

End of NSIndexSet countOfIndexesInRange example article.