How to use NSIndexSet
Asked Answered
H

4

32

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.

Hardiman answered 19/12, 2010 at 23:35 Comment(0)
S
82

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.

Sava answered 26/4, 2012 at 15:16 Comment(2)
+1 for using NSMakeRange(). Instinct is to look for something like NSRangeMake(), which unfortunately doesn't exist.Irremissible
dear Down-voters! I would have been honoured to get some feedback about why your hands were shaking above the down-voter button and eventually click on it, because – you believe or not – I'm not a mind reader to figure out it. thank you!Sava
L
5

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

Lutes answered 19/12, 2010 at 23:49 Comment(2)
An index set is not necessarily a single contiguous range. You can create an immutable index set with a single index or with a range, but you can use a mutable index set to create an index set covering any combination of zero or more single indexes and zero or more ranges—and, of course, you can then make an immutable copy of an index set so customized.Daphene
Absolutely; I specifically said that they are internally NSRanges in the plural.Lutes
M
4

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

[myTableView selectRowIndexes:[NSIndexSet indexSetWithIndex:indexes] 
         byExtendingSelection:NO];
Mezzosoprano answered 20/12, 2010 at 0:36 Comment(0)
D
0

I seem to need the code

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

No; those are casts without anything to cast, which is invalid.

Remove the casts and put values there instead.

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

Yes, because you don't want to extend the selection, you want to replace it.

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

The same as for passing any other variable or message expression.

You need to create an index set and then either stash it in a variable and pass that or pass the result of the creation message directly.

Daphene answered 20/12, 2010 at 1:5 Comment(1)
re: casting without anything to cast: I think he was just copying (or visually duplicating) the placeholder code that Xcode generates.Lutes

© 2022 - 2024 — McMap. All rights reserved.