How to use NSTableView selectedRowIndexes?

I am just learning basic programming in Objective-C and Cocoa. I am trying to get some data from an NSTableView. Based on what I read in one tutorial, I wrote the following:

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

But then I found out that selectedRowEnumerator deprecated already in 10.3 Panther and that I should use selectedRowIndexes .

The problem is that I did not find how to actually use the returned NSIndexSet to achieve the same result as with the code written above.

So, if anyone could give me advice, I would be very grateful. Thanks.

+4
source share
2 answers

You can scroll through the NSIndexSet indices as follows:

 - (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 } 
+2
source

To illustrate most clearly,

  NSIndexSet *selectedRows = [MyTableView selectedRowIndexes]; NSUInteger numberOfSelectedRows = [selectedRows count]; NSUInteger indexBuffer[numberOfSelectedRows]; NSUInteger limit = [selectedRows getIndexes:indexBuffer maxCount:numberOfSelectedRows inIndexRange:NULL]; for (unsigned iterator = 0; iterator < limit; iterator++) { MyObject *object = [ myDataSource objectAtIndex:indexBuffer[iterator] ]; NSLog(@"Object at index: %lu", (unsigned long) indexBuffer[iterator] ); NSLog(@"Object selected: %@", [object description]); } 
+2
source

Source: https://habr.com/ru/post/1309906/


All Articles