Jakub Lédl Jakub Lédl - 2 months ago 19
Objective-C Question

How to use NSTableView's selectedRowIndexes?

I'm just learning some basic programming in Objective C and Cocoa. I'm trying to get some data from NSTableView. Based on what I read in one tutorial, I wrote this:

NSArray * items = [[itemsTableView selectedRowEnumerator] allObjects];


But then I learned that
selectedRowEnumerator
was deprecated already in 10.3 Panther and that I should use
selectedRowIndexes
.

The problem is, I didn't find how to actually use the returned
NSIndexSet
to achieve the same result as with code written above.

So, If anyone could give me a tip, I would be very grateful. Thanks.

Answer

You can loop through the indexes of an NSIndexSet like this:

- (void) goThroughIndexSet:(NSIndexSet *) anIndexSet
{
    NSUInteger idx = [anIndexSet firstIndex];

    while (idx != NSNotFound)
    {
        // do work with "idx"
        NSLog (@"The current index is %u", idx);

        // get the next index in the set
        idx = [anIndexSet indexGreaterThanIndex:idx];
    }

    // all done
}
Comments