Reputation: 431
I am trying to figure out how to create a set of indexes of lets say (1, 2, 3) then use it in
- (void)selectRowIndexes:(NSIndexSet *)indexes byExtendingSelection:(BOOL)extend
It's the (NSIndexSet *)indexes
I do not know how to use / create / populate with indexes 1, 2, 3.
Should I use a class method or instance method?
I tried a whole bunch of ways but I have no idea what I'm doing ...
Upvotes: 43
Views: 24904
Reputation: 11073
In Swift 3
let indexSet = IndexSet(integersIn: 1...3)
or for a single value:
let indexSet = IndexSet(integer: 1)
or for non consecutive values:
var indexSet = IndexSet()
indexSet.insert(1)
indexSet.insert(3)
indexSet.insert(9)
Upvotes: 3
Reputation: 15951
Here is a example to delete selected index from the Local Array with the help of NSMutableIndexSet
NSArray * arrSelectedSongs = [self.playListTblView indexPathsForSelectedRows];
//Delete From Array
NSMutableIndexSet *indexSet = [[NSMutableIndexSet alloc]init];
for (NSIndexPath *indexPath in arrSelectedSongs)
{
[indexSet addIndex:indexPath.row];
}
[self.arrFiles removeObjectsAtIndexes:indexSet];
Upvotes: 0
Reputation: 135548
If the indexes are consecutive like in your example, you can use this:
NSIndexSet *indexSet = [NSIndexSet indexSetWithIndexesInRange:NSMakeRange(1, 3)];
If not, create a mutable index set and add the indexes (or ranges) one by one:
NSMutableIndexSet *indexSet = [NSMutableIndexSet indexSet];
[indexSet addIndex:3];
[indexSet addIndex:5];
[indexSet addIndex:8];
Upvotes: 76
Reputation: 39905
For simple index sets which contain a single index or a group of indexes which can be represented by a range (i.e. 1,2,3), you can create them using NSIndexSet's initWithIndex:
or initWithIndexesInRange:
methods (or their factory method counterparts). If you want to create a more complex index set (i.e. 1,5,7,8,9), you will need to use NSMutableIndexSet so that you can add indexes and ranges after you create the object.
This code will create an index set containing the indexes 1, 2, and 3:
[[NSIndexSet alloc] initWithIndexesInRange:NSMakeRange(1,3)];
See the NSIndexSet and NSMutableIndexSet class references.
Upvotes: 7