Wednesday, June 19, 2013

NSIndexPath indexPathWithIndex example in Objective C (iOS).


NSIndexPath indexPathWithIndex

Creates an one-node index path.

+ (id)indexPathWithIndex:(NSUInteger)index

Parameters
index
Index of the item in node 0 to point to.

Return Value of [NSIndexPath indexPathWithIndex]
One-node index path with index.

NSIndexPath indexPathWithIndex example.
You might use it like this:

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

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

Example of [NSIndexPath indexPathWithIndex].
For an int index:

NSIndexPath *path = [NSIndexPath indexPathWithIndex:index];
Creates Index of the item in node 0 to point to as per the reference.

To use the indexPath in a UITableView, the more appropriate method is

NSIndexPath *path = [NSIndexPath indexPathForRow:row inSection:section];

NSIndexPath indexPathWithIndex example.
If you have no sections, then you can try the indexPathWithIndex: class constructor:

[self.tableView scrollToRowAtIndexPath:[NSIndexPath indexPathWithIndex:1]
                      atScrollPosition:UITableViewScrollPositionNone
                              animated:NO];

End of NSIndexPath indexPathWithIndex example article.