Ronnie Liew
Ronnie Liew

Reputation: 18270

How to create the "indexes" required for NSIndexPath:indexPathWithIndexes:length:

The class method to create an index path with one or more nodes is:

+ (id)indexPathWithIndexes:(NSUInteger *)indexes length:(NSUInteger)length

How do we create the "indexes" required in the first parameter?

The documentation listed it as Array of indexes to make up the index path but it is expecting a (NSUinteger *).

To create an index path of 1.2.3.4, is it simply an array of [1,2,3,4] ?

Upvotes: 42

Views: 44006

Answers (4)

jungledev
jungledev

Reputation: 4345

I did this in 2 lines of code

 NSMutableArray *indexPaths = [[NSMutableArray alloc] init];   
 for (int i = firstIndexYouWant; i < totalIndexPathsYouWant; i++) [indexPaths addObject:[NSIndexPath indexPathForRow:i inSection:0]];

Short, clean, and readable. Free code, don't knock it.

Upvotes: 3

Alpar Katona
Alpar Katona

Reputation:

On iOS, you can also use this method from NSIndexPath UIKit Additions (declared in UITableView.h):

+ (NSIndexPath*) indexPathForRow:(NSUInteger)row inSection:(NSUInteger)section

Upvotes: 43

Barry Wark
Barry Wark

Reputation: 107754

You are correct. You might use it like this:

NSUInteger indexArr[] = {1,2,3,4};

NSIndexPath *indexPath = [NSIndexPath indexPathWithIndexes:indexArr length:4];

Upvotes: 61

Giao
Giao

Reputation: 15146

You assumption is correct. It's as simple as a C array of NSUInteger. The length parameter is the number of elements in the indexes array.

Arrays in C are often identified as a pointer (in this case NSUInteger *) with a length parameter or a known terminator such as \0 for C strings (which is just a char array).

Upvotes: 7

Related Questions