Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

How to use NSIndexSet

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.

like image 736
hertopnerd Avatar asked Dec 19 '10 23:12

hertopnerd


People also ask

What is NSIndexSet?

The NSIndexSet class represents an immutable collection of unique unsigned integers, known as indexes because of the way they are used. This collection is referred to as an index set. Indexes must be in the range 0 .. NSNotFound - 1 . You use index sets in your code to store indexes into some other data structure.

What is an index set Swift?

IndexSet is simply Set of all indexes of the elements in the array to remove.


2 Answers

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.

like image 166
holex Avatar answered Sep 21 '22 21:09

holex


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).

like image 33
Richard Avatar answered Sep 21 '22 21:09

Richard