How to use NSIndexSet

46,478

Solution 1

it would be the proper way:

NSIndexSet *indexSet = [NSIndexSet indexSetWithIndexesInRange:NSMakeRange(0, 3)];

or you can use the NSMutableIndexSet for the random indexes:

NSMutableIndexSet *mutableIndexSet = [[NSMutableIndexSet alloc] init];
[mutableIndexSet addIndex:0];
[mutableIndexSet addIndex:2];
[mutableIndexSet addIndex:9];

etc.

Solution 2

Printing out an NSIndexSet in the debugger will show you that they are internally NSRanges. To create one, you can either specify the range or a single explicit index (from which it will create the range); something like

NSIndexSet *indexes = [[NSIndexSet alloc] initWithIndex:rowToHighlight];
[myTableView selectRowIndexes:indexes byExtendingSelection:NO];
[indexes release];

Note that the index(es) must all be unsigned integers (NSUIntegers, specifically).

Solution 3

I'd use a factory method to avoid having to manage memory:

[myTableView selectRowIndexes:[NSIndexSet indexSetWithIndex:indexes] 
         byExtendingSelection:NO];
Share:
46,478
hertopnerd
Author by

hertopnerd

interested in mathematics, programming, data, woodworking

Updated on July 15, 2020

Comments

  • hertopnerd
    hertopnerd almost 4 years

    In Objective-C, my program opens a window and displays a table. I want to have a specified row of the table highlighted.

    How do I do this?

    I seem to need the code

    [myTableView selectRowIndexes:(NSIndexSet *) byExtendingSelection:(BOOL)];
    

    I looked at the developer documentation, and figured out that the BOOL should be NO.

    By looking at the NSIndexSet docs, I can't figure out what the right syntax should be.