NSIndexpath.item vs NSIndexpath.row

Does anyone know the difference between NSIndexpath.row and NSIndexpath.item ?

In particular, which one do I use in:

 -(UITableViewCell *)tableView:(UITableView *)tableView cellForRowAtIndexPath:(NSIndexPath *)indexPath; 
+50
ios uitableview nsindexpath
Feb 08 '13 at 4:34
source share
5 answers

Well, no one gave a good answer here.

Inside NSIndexPath, 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 for _indexes [0], and item and row are aliases for _indexes [1]. Thus, they are functionally identical.

In terms of programming style, and possibly the definition chain, you'd better use a “row” in the context of tables and an “element” in the context of collections.

+114
Jun 19 '13 at 3:03 on
source share
 indexPath.row is best in your case 

First information about NSIndexPath

The NSIndexPath class represents the path to a specific node in a tree of nested array arrays. This path is known as a pointer path.

Each index in indexPath represents an index into an array of children from one node in the tree to another, deeper than the node.

For example , indexPath 1.4.3.2 indicates the path shown in the figure. enter image description here

Here, in your case, indexPath.row returns the row index in a particular indexPath .

Differences between indexPath.row and indexPath.item

Normally indexPath has two properties

1 - line

2 - element

string - used with a UITableView to get a specific base row on indexPath. this is also a read only property

  Available in iOS 2.0 and later. 

item - use correctly with UICollectionView to get an item in a section. This property is read only. To use this property, you need to declare it in
UICollectionView.h

 > Available in iOS 6.0 and later. 
+25
Feb 08 '13 at 4:40
source share

You need to use indexPath.row

The difference is that:

indexPath.row for tableView and indexPath.item is for the View collection .

element

The index number identifying the item in the collection view. (read-only) @property (nonatomic, readonly) NSInteger item;

Discussion

The section in which the item is located is identified by the value of the section. Availability

 Available in iOS 6.0 and later. 

Announced in UICollectionView.h




line

The index number that identifies the row in the table view section. (read-only) @property(nonatomic, readonly) NSInteger row;

Discussion

The section in which the string is located is identified by the value of the section. Availability

 Available in iOS 2.0 and later. 

Read more about NSIndexPath Add-ons

+9
08 Feb '13 at 4:38
source share

@Owen Godfrey's answer is better than the accepted answer from @iPatel. Here is another clarification that I could not insert in the commentary on his answer, so I will copy his answer and add it here. The loan belongs to Owen.




From @Owen Godfrey:

Inside NSIndexPath, 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 of _indexes [0], and both item and row are aliases for _indexes 1 , so they are functionally identical.

In terms of programming style, and possibly the definition chain, you'd better use a “row” in the context of tables and an “element” in the context of collections.




The main interface of NSIndexPath is defined in NSIndexPath.h. Index storage is in _indexes, which is a private one-dimensional array of NSUInteger. NSIndexPath itself can represent any number of dimensions. There are two corresponding categories on NSIndexPath that extend the functionality: one of the UICollectionView.h "NSIndexPath (UICollectionViewAdditions)" and one of the UITableView.h "NSIndexPath (UITableView)". One of UICollectionView.h adds the "item" readonly property and associated convenience methods. One of UITableView.h adds the readonly string property and its convenience methods. However, both properties are simply shells that access the base value in _indexes [1].

Because UIKit associates with both categories, both sets of handy features are always available, regardless of where you use them on iOS. Therefore, you can create NSIndexPath from [NSIndexPath indexPathForRow: inSection:], but get the second index from indexPath.item. The base value is exactly the same, regardless of whether it turned to indexPath.item or indexPath.row.

Stylistically, it’s cleaner if you use an “element” with a UICollectionView and a “row” with a UITableView, as they are intended to be used, and this makes the code more readable. However, your program will not crash if you replace them.

Link: NSIndexPath

+1
Dec 22 '15 at 10:26
source share

Look at the bottom of UICollectionView.h and you will see a category that extends NSIndexPath to add item as a property when used inside instances of UICollectionView.

At the bottom of UITableView.h there is a similar section that adds row and section properties for NSIndexPaths, which are used in UITableViews.

If you are trying to access these properties of an instance of NSIndexPath in a class, and NSIndexPathInstance does not think that they are, just import the class header that defines them at the top of your class and you will magically access these properties.

UICollectionView.h

 @interface NSIndexPath (UICollectionViewAdditions) + (instancetype)indexPathForItem:(NSInteger)item inSection:(NSInteger)section NS_AVAILABLE_IOS(6_0); @property (nonatomic, readonly) NSInteger item NS_AVAILABLE_IOS(6_0); @end 

UITableView.h

 //_______________________________________________________________________________________________________________ // This category provides convenience methods to make it easier to use an NSIndexPath to represent a section and row @interface NSIndexPath (UITableView) + (instancetype)indexPathForRow:(NSInteger)row inSection:(NSInteger)section; @property (nonatomic, readonly) NSInteger section; @property (nonatomic, readonly) NSInteger row; @end 

To use these properties inside your class, you will need to import the required text into your class as follows:

@import "UIKit/UITableView.h"

And then you can do things like: myIndexPath.row and [myIndexPath row]

0
Mar 18 '16 at 17:20
source share



All Articles