NSIndexpath.item vs NSIndexpath.row
Solution 1
Okay, nobody has given a good answer here.
Inside NSIndexPath, the indexes are stored in a simple c array called "_indexes" defined as NSUInteger* and the length of the array is stored in "_length" defined as NSUInteger. The accessor "section" is an alias to "_indexes[0]" and both "item" and "row" are aliases to "_indexes[1]". Thus the two are functionally identical.
In terms of programming style – and perhaps the definition chain – you would be better using "row" in the context of tables, and "item" in the context of collections.
Solution 2
indexPath.row is best in your case
First info about NSIndexPath
The NSIndexPath
class represents the path to a specific node in a tree of nested array collections. This path is known as an index path.
Each index in an indexPath represents the index into an array of children from one node in the tree to another, deeper node.
For example, the indexPath 1.4.3.2 specifies the path shown in Figure
Here in your case indexPath.row
returns the index of the row at the specific indexPath
.
Differences between indexPath.row and indexPath.item
Generally indexPath
has two properties
1 - row
2 - item
row - property use with UITableView
for get specific row base on indexPath.
it is also read only property
Available in iOS 2.0 and later.
item - properly use with UICollectionView
for get item in section.
It is a read-only property. To use this property you need to declare it in
UICollectionView.h
> Available in iOS 6.0 and later.
Solution 3
You need to use indexPath.row
Difference is that:
indexPath.row is for tableView and indexPath.item is for collectionView.
item
An index number identifying an item in a section of a collection view. (read-only)
@property (nonatomic, readonly) NSInteger item;
Discussion
The section the item is in is identified by the value of section. Availability
Available in iOS 6.0 and later.
Declared In UICollectionView.h
row
An index number identifying a row in a section of a table view. (read-only)
@property(nonatomic, readonly) NSInteger row;
Discussion
The section the row is in is identified by the value of section. Availability
Available in iOS 2.0 and later.
Please check the NSIndexPath Additions for details
Solution 4
@Owen Godfrey's answer is better than the accepted answer from @iPatel. Here is some further clarification which I wasn't able to fit into a comment on his answer, so I'll copy his answer and add to it here. Credit belongs to Owen.
From @Owen Godfrey:
Inside NSIndexPath, the indexes are stored in a simple c array called "_indexes" defined as NSUInteger* and the length of the array is stored in "_length" defined as NSUInteger. The accessor "section" is an alias to "_indexes[0]" and both "item" and "row" are aliases to "_indexes1". Thus the two are functionally identical.
In terms of programming style – and perhaps the definition chain – you would be better using "row" in the context of tables, and "item" in the context of collections.
The core interface of NSIndexPath is defined in NSIndexPath.h. The storage of the indexes is in _indexes which is a private one-dimensional array of NSUInteger. NSIndexPath by itself can represent any number of dimensions. There are two relevant categories on NSIndexPath which extend the functionality, one from UICollectionView.h "NSIndexPath (UICollectionViewAdditions)" and one from UITableView.h "NSIndexPath (UITableView)". The one from UICollectionView.h adds the readonly property "item" and related convenience methods. The one from UITableView.h adds the readonly property "row" and related convenience methods. However both properties are just wrappers that access the underlying value in _indexes[1].
Since UIKit links with both categories, both sets of convenience functions are always available, no matter where in IOS you are using them. So you could create an NSIndexPath from [NSIndexPath indexPathForRow:inSection:] but retrieve the second index from indexPath.item. The underlying value is exactly the same whether accessed by indexPath.item or indexPath.row.
Stylistically it is cleaner if you use "item" with UICollectionView and "row" with UITableView as that is how they were intended to be used, and this makes for more readable code. However your program won't crash if you interchange them.
Reference: NSIndexPath
Related videos on Youtube
Cyberpass
Updated on May 06, 2020Comments
-
Cyberpass about 4 years
Does anyone know the difference between
NSIndexpath.row
andNSIndexpath.item
?Specifically, which one do I use in:
-(UITableViewCell *)tableView:(UITableView *)tableView cellForRowAtIndexPath:(NSIndexPath *)indexPath;
-
holex over 8 yearsyes, the
row
is used byUITableView
rows; theitem
is used byUICollectionView
cells.
-
-
esh over 11 yearsYou could have just given the NSIndexPath Class Reference link, y'know?
-
Rob over 11 yearsPerhaps, but it's generally advised to include relevant portions in the answer itself. Given that the other answers gave too narrow of an answer, I appreciate someone who points out the broader use of
NSIndexPath
. -
esh over 11 yearsI still see no mention of indexPath.item, whereas one of the things the question was asking was the difference between indexPath.item and indexPath.row. Midhun MP's answer does that. Just copy-pasting documentation doesn't help much without some amount of elaboration.