NSIndexpath.item与NSIndexpath.row

有谁知道NSIndexpath.rowNSIndexpath.item之间的区别?

具体来说,我使用哪一个:

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

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

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

在编程风格方面 – 也许是定义链 – 你最好在表的上下文中使用“row”,在集合的上下文中使用“item”。

 indexPath.row is best in your case 

有关NSIndexPath的第一个信息

NSIndexPath类表示嵌套数组集合树中特定节点的path。 这条path被称为索引path。

indexPath中的每个索引都表示从树中的一个节点到另一个较深节点的子数组的索引。

例如 ,indexPath 1.4.3.2指定了图中所示的path 在这里输入图像说明

这里你的情况indexPath.row返回特定indexPath的行的索引。

indexPath.row and indexPath.item 之间的 indexPath.row and indexPath.item

一般indexPath两个属性

1 –

2 – 项目

行 –属性使用UITableView获取特定的基于indexPath。 它也是只读属性

  Available in iOS 2.0 and later. 

item –正确使用UICollectionView获取项目的部分。 这是一个只读属性。 要使用这个属性,你需要声明它
UICollectionView.h

 > Available in iOS 6.0 and later. 

你需要使用indexPath.row

不同之处在于:

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

项目

标识集合视图的一部分中的项目的索引号。 (只读) @property (nonatomic, readonly) NSInteger item;

讨论

项目所在的部分由section的值标识。 可用性

 Available in iOS 6.0 and later. 

在UICollectionView.h中声明


标识表格视图的一部分中的行的索引编号。 (只读) @property(nonatomic, readonly) NSInteger row;

讨论

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

 Available in iOS 2.0 and later. 

详情请查阅NSIndexPath Additions

@Owen Godfrey的答案比@iPatel的答案要好。 这里有一些进一步的说明,我没有把他的答案放在评论中,所以我会复制他的答案,并在这里添加它。 信用属于欧文。


从@Owen Godfrey:

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

在编程风格方面 – 也许是定义链 – 你最好在表的上下文中使用“row”,在集合的上下文中使用“item”。


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

由于UIKit与两个类别都链接,因此无论您在哪里使用IOS,便可始终使用这两组便利function。 所以你可以从[NSIndexPath indexPathForRow:inSection:]创build一个NSIndexPath,但从indexPath.item中检索第二个索引。 无论由indexPath.item还是indexPath.row访问,底层值都是完全相同的。

在风格上,如果你使用UICollectionView中的“item”和UITableView中的“row”作为它们打算被使用的方式,那么它会更干净。 但是,如果你交换它们,你的程序不会崩溃。

参考: NSIndexPath

查看UICollectionView.h的底部,在UICollectionView实例中使用时,您将看到扩展NSIndexPath以将item添加为属性的类别。

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

如果您试图在类中访问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 

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 

要在你的类中使用这些属性,你必须像你这样将所需的类导入到你的类中:

@import "UIKit/UITableView.h"

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