Creating Index Paths

+ indexPathWithIndex:

Creates an one-node index path.

Example

NSIndexPath *indexPath = [NSIndexPath indexPathWithIndex:4];
NSLog(@"%@",indexPath);		
	

Output

2014-04-12 17:21:53.627 iOS-Tutorial[641:a0b]  {length = 1, path = 4}		

+ indexPathWithIndexes:length:

Creates an index path with one or more nodes.

Example

NSUInteger indexes[] = {2,4,5,6};
NSIndexPath *indexPath = [NSIndexPath indexPathWithIndexes:indexes length:4];
NSLog(@"%@",indexPath);
		

Output

2014-04-12 17:26:03.020 iOS-Tutorial[663:a0b]  {length = 4, path = 2 - 4 - 5 - 6}
		

- initWithIndex:

Initializes an allocated NSIndexPath (page 814) object with a one-node index path.

Example

NSIndexPath *indexPath = [[NSIndexPath alloc] initWithIndex:5];
NSLog(@"%@",indexPath);
		

Output

2014-04-12 17:27:42.335 iOS-Tutorial[691:a0b]  {length = 1, path = 5}
		

- initWithIndexes:length:

Initializes an allocated NSIndexPath (page 814) object with an index path of a specific length.

Example

NSUInteger indexes[] = {2,4,5,6};
NSIndexPath *indexPath = [[NSIndexPath alloc] initWithIndexes:indexes length:4];
NSLog(@"%@",indexPath);
		

Output

2014-04-12 17:27:04.156 iOS-Tutorial[678:a0b]  {length = 4, path = 2 - 4 - 5 - 6}
		

Querying Index Paths

- indexAtPosition:

Provides the index at a particular node in the index path.

Example

NSUInteger indexes[] = {2,4,5,6};
NSIndexPath *indexPath = [[NSIndexPath alloc] initWithIndexes:indexes length:4];
NSLog(@"%d",[indexPath indexAtPosition:2]);
		

Output

2014-04-12 17:30:46.670 iOS-Tutorial[717:a0b] 5
		

- indexPathByAddingIndex:

Provides an index path containing the indexes in the receiving index path and another index.

Example

NSUInteger indexes[] = {2,4,5,6};
NSIndexPath *indexPath = [[NSIndexPath alloc] initWithIndexes:indexes length:4];
NSLog(@"%@",[indexPath indexPathByAddingIndex:2]);
		

Output

2014-04-12 17:31:22.942 iOS-Tutorial[730:a0b]  {length = 5, path = 2 - 4 - 5 - 6 - 2}
		

- indexPathByRemovingLastIndex

Provides an index path with the indexes in the receiving index path, excluding the last one.

Example

NSUInteger indexes[] = {2,4,5,6};
NSIndexPath *indexPath = [[NSIndexPath alloc] initWithIndexes:indexes length:4];
NSLog(@"%@",[indexPath indexPathByRemovingLastIndex]);
		

Output

2014-04-12 17:32:17.821 iOS-Tutorial[745:a0b]  {length = 3, path = 2 - 4 - 5}
		

- length

Provides the number of indexes in the index path.

Example

NSUInteger indexes[] = {2,4,5,6};
NSIndexPath *indexPath = [[NSIndexPath alloc] initWithIndexes:indexes length:4];
NSLog(@"%d",[indexPath length]);
		

Output

2014-04-12 17:32:49.137 iOS-Tutorial[760:a0b] 4
		

- getIndexes:

Copies the objects contained in the index path into indexes.

Example

NSUInteger indexes[] = {2,4,5,6};
NSIndexPath *indexPath = [[NSIndexPath alloc] initWithIndexes:indexes length:4];
NSUInteger newIndexes[[indexPath length]];
[indexPath getIndexes:newIndexes];
NSLog(@"%d",newIndexes[3]);
		

Output

2014-04-12 17:38:19.262 iOS-Tutorial[824:a0b] 6
		

Comparing Index Paths

- compare:

Indicates the depth-first traversal order of the receiving index path and another index path.

Example

NSIndexPath *indexPath1 = [[NSIndexPath alloc] initWithIndex:2];
NSIndexPath *indexPath2 = [[NSIndexPath alloc] initWithIndex:2];
NSIndexPath *indexPath3 = [[NSIndexPath alloc] initWithIndex:3];
NSComparisonResult result = [indexPath1 compare:indexPath2];
NSLog(@"%d",result);
result = [indexPath3 compare:indexPath2];
NSLog(@"%d",result);
		

Output

2014-04-12 17:44:13.286 iOS-Tutorial[867:a0b] 0
2014-04-12 17:44:13.287 iOS-Tutorial[867:a0b] 1
		

Advertisements