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

Viewed 43874

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] ?

4 Answers

You are correct. You might use it like this:

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

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

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

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

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).

Related