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.

RK-
  • 12,099
  • 23
  • 89
  • 155
hertopnerd
  • 618
  • 1
  • 5
  • 9

4 Answers4

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.

HotFudgeSunday
  • 1,403
  • 2
  • 23
  • 29
holex
  • 23,961
  • 7
  • 62
  • 76
  • 1
    +1 for using `NSMakeRange()`. Instinct is to look for something like `NSRangeMake()`, which unfortunately doesn't exist. – Josh Kovach Feb 26 '13 at 19:49
  • 16
    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! – holex Aug 18 '14 at 13:41
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).

Richard
  • 3,316
  • 30
  • 41
  • 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. – Peter Hosey Dec 20 '10 at 01:41
  • Absolutely; I specifically said that they are internally NSRanges in the plural. – Richard Dec 20 '10 at 15:27
4

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

[myTableView selectRowIndexes:[NSIndexSet indexSetWithIndex:indexes] 
         byExtendingSelection:NO];
Paul Schreiber
  • 12,531
  • 4
  • 41
  • 63
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.

Peter Hosey
  • 95,783
  • 15
  • 211
  • 370
  • re: casting without anything to cast: I think he was just copying (or visually duplicating) the placeholder code that Xcode generates. – Richard Dec 20 '10 at 01:38