How to get specific values ​​from NSIndexPath

I have an NSArray with NSIndexPaths inside it

NSArray *array = [self.tableView indexPathsForSelectedRows]; for (int i = 0; i < [array count]; i++) { NSLog(@"%@",[array objectAtIndex:i]); } 

NSLog returns this:

 <NSIndexPath 0x5772fc0> 2 indexes [0, 0] <NSIndexPath 0x577cfa0> 2 indexes [0, 1] <NSIndexPath 0x577dfa0> 2 indexes [0, 2] 

I am trying to get the second value from indexPath only in a simple NSInteger

+6
source share
1 answer

You can use the NSIndexPath -indexAtPosition: method to get the latest index:

 NSIndexPath *path = ...; // Initialize the path. NSUInteger lastIndex = [path indexAtPosition:[path length] - 1]; // Gets you the '2' in [0, 2] 

In your case, you can use the following (as Josh noted in his comment, I assume that you are working with a custom subclass of UITableView because it does not have this particular method ( -indexPathsForSelectedRows: ):

 NSArray *indexes = [self.tableView indexPathsForSelectedRows]; for (NSIndexPath *path in indexes) { NSUInteger index = [path indexAtPosition:[path length] - 1]; NSLog(@"%lu", index); } 

This will print 0, 1, 2, ...

+10
source

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


All Articles