Ios nsindepath.item vs nsindepath.row

Ios nsindepath.item vs nsindepath.row,ios,uitableview,nsindexpath,Ios,Uitableview,Nsindexpath,有人知道nsindepath.row和nsindepath.item之间的区别吗 具体来说,我应该在以下方面使用哪一个: -(UITableViewCell *)tableView:(UITableView *)tableView cellForRowAtIndexPath:(NSIndexPath *)indexPath; 您需要使用indexPath.row 不同之处在于: indexPath.row用于tableView,indexPath.item用于collectionView 项目

有人知道
nsindepath.row
nsindepath.item
之间的区别吗

具体来说,我应该在以下方面使用哪一个:

-(UITableViewCell *)tableView:(UITableView *)tableView cellForRowAtIndexPath:(NSIndexPath *)indexPath;

您需要使用
indexPath.row

不同之处在于:

indexPath.row用于tableView,indexPath.item用于collectionView

项目

标识集合视图某个部分中的项的索引号。 (只读)
@property(非原子,只读)集成项

讨论

项目所在的节由节的值标识。 可用性

Available in iOS 6.0 and later.
Available in iOS 2.0 and later.
在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

标识表视图中某一节中某一行的索引号。 (只读)
@property(非原子,只读)集成器行

讨论

行所在的节由节的值标识。 可用性

Available in iOS 6.0 and later.
Available in iOS 2.0 and later.
请查看详细信息

indexPath.row is best in your case 
关于nsindepath的第一条信息

>     Available in iOS 6.0 and later.
类表示嵌套数组集合树中特定节点的路径。此路径称为索引路径

indexPath中的每个索引将索引表示为从树中的一个节点到另一个更深节点的子节点数组

例如,indexPath1.4.3.2指定了如图所示的路径

在本例中,
indepath.row
返回特定
indepath
处的行索引

indexPath.row和indexPath.item之间的差异

通常,
indexPath
有两个属性

1-

2-项目

行-属性与
UITableView一起使用,用于基于indexPath获取特定的行。
它也是只读属性

项目-正确使用
UICollectionView
以获取第节中的项目。 它是只读属性。要使用此属性,您需要在
UICollectionView.h

>     Available in iOS 6.0 and later.

好的,这里没有人给出一个好的答案

在nsindepath内部,索引存储在一个名为“_index”的简单c数组中,该数组定义为NSUInteger*,数组的长度存储在定义为NSUInteger*的“_length”中。访问器“section”是“_索引[0]”的别名,“item”和“row”都是“_索引[1]”的别名。因此,两者在功能上是相同的


就编程风格而言——也许还有定义链——您最好在表的上下文中使用“行”,在集合的上下文中使用“项”。

@Owen Godfrey的答案比@iPatel接受的答案要好。这里有一些进一步的澄清,我无法将其纳入对他的答案的评论中,所以我将复制他的答案并添加到这里。荣誉属于欧文


来自@Owen Godfrey:

在nsindepath内部,索引存储在一个名为“_index”的简单c数组中,该数组定义为NSUInteger*,数组的长度存储在定义为NSUInteger*的“_length”中。访问器“section”是“\u索引[0]”的别名,“item”和“row”都是“\u索引”的别名。因此,两者在功能上是相同的

就编程风格而言——也许还有定义链——最好在表的上下文中使用“行”,在集合的上下文中使用“项”


nsindepath的核心接口在nsindepath.h中定义。索引存储在_索引中,这是一个私有的一维整数数组。nsindepath本身可以表示任意数量的维度。NSIndexPath上有两个相关类别扩展了该功能,一个来自UICollectionView.h“NSIndexPath(UICollectionViewAdditions)”,另一个来自UITableView.h“NSIndexPath(UITableView)”。UICollectionView.h中的一个添加了只读属性“item”和相关的便利方法。UITableView.h中的一个添加了只读属性“row”和相关的便利方法。但是,这两个属性只是访问_索引[1]中的基础值的包装器

由于UIKit与这两个类别都有链接,因此无论您在IOS中的何处使用,这两组便利功能都始终可用。因此,您可以从[nsindepath indexPathForRow:instation:]创建nsindepath,但从indepath.item检索第二个索引。无论是通过indexPath.item还是indexPath.row访问,基础值都完全相同

从风格上讲,如果将“item”与UICollectionView一起使用,将“row”与UITableView一起使用,则会更简洁,因为这是它们的预期使用方式,这使得代码更具可读性。但是,如果您交换它们,您的程序不会崩溃


参考资料:

查看UICollectionView.h的底部,您将看到一个类别,该类别扩展了NSIndexPath,以便在中为UICollectionView实例添加
项作为属性

在UITableView.h的底部有一个类似的部分,它为UITableView中使用的nsindexpath添加了
部分
属性

如果您试图访问类中NSIndexPath实例的这些属性,而NSIndexPathInstance不相信它们存在,只需将定义它们的类的头导入到类的顶部,您就可以神奇地访问这些属性

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
UtableView.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
要在类中使用这些属性,必须将所需的属性导入到类中,如下所示:

@import“UIKit/UITableView.h”

然后你可以做这样的事情:
myIndexPath.row
[myIndexPath row]

你可以