NSIndexPath
NSIndexPath represents the position of an item in a list: item number item in section number section, both counted from 0.
A position changes with the data: insert a row before an item and its position changes. To remember an item over time, store its identifier and convert it to a position when needed with the data source's indexPathForItemIdentifier().
NSIndexPath is a read-only value of the same kind as structs:
- Fields are read-only. Writing throws
TypeError. - Both numbers are non-negative integers. Fractions are truncated toward zero; negative numbers and values that can't be converted to numbers throw
TypeError. - Parameters that take a position also accept a plain object.
{ section: 0, item: 3 }is equivalent tonew NSIndexPath(0, 3). What you read back is always anNSIndexPath. - Converted to a string, it is
'NSIndexPath(0, 3)'.
Inheritance: NSIndexPath. It does not inherit from NSObject.
Constructor
Creates a position from a section number and an item number. The section comes first, then the item.
Static methods
NSIndexPath.indexPathForItem()
Same order as in ObjC: the item first, then the section.
Same as the previous one, named with row.
Instance properties
| Property | Description |
|---|---|
section read-only | The section number, counted from 0 |
item read-only | The item number within the section, counted from 0 |
row read-only | The same number as item |
Examples
const a = new NSIndexPath(0, 3);
const b = NSIndexPath.indexPathForItem(3, { inSection: 0 });
console.log(String(a), a.section === b.section && a.item === b.item);Result: logs NSIndexPath(0, 3) true.
Corresponding iOS API
| ObjC | JS |
|---|---|
[NSIndexPath indexPathForItem:3 inSection:0] | new NSIndexPath(0, 3), NSIndexPath.indexPathForItem(3, { inSection: 0 }), or { section: 0, item: 3 } |
[NSIndexPath indexPathForRow:3 inSection:0] | NSIndexPath.indexPathForRow(3, { inSection: 0 }) |
path.section, path.item, path.row | Same names |
The arguments of new NSIndexPath(section, item) are section first, then item, the opposite of ObjC's indexPathForItem:inSection:. ObjC's arbitrary-length NSIndexPath (indexPathWithIndexes:length:) is not provided.