ios NSIndexpath.item 与 NSIndexpath.row

声明:本页面是StackOverFlow热门问题的中英对照翻译,遵循CC BY-SA 4.0协议,如果您需要使用它,必须同样遵循CC BY-SA许可,注明原文地址和作者信息,同时你必须将它归于原作者(不是我):StackOverFlow 原文地址: http://stackoverflow.com/questions/14765730/
Warning: these are provided under cc-by-sa 4.0 license. You are free to use/share it, But you must attribute it to the original authors (not me): StackOverFlow

提示:将鼠标放在中文语句上可以显示对应的英文。显示中英文
时间:2020-08-30 22:13:02  来源:igfitidea点击:

NSIndexpath.item vs NSIndexpath.row

iosuitableviewnsindexpath

提问by Cyberpass

Does anyone know the difference between NSIndexpath.rowand NSIndexpath.item?

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

Specifically, which one do I use in:

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

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

回答by Owen Godfrey

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.

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

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.

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

回答by iPatel

indexPath.row is best in your case 

First info about NSIndexPath

关于 NSIndexPath 的第一个信息

The NSIndexPathclass represents the path to a specific node in a tree of nested array collections. This path is known as an index path.

所述NSIndexPath类表示嵌套数组集合树的路径的特定节点。此路径称为索引路径。

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

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

For example, the indexPath 1.4.3.2specifies the path shown in Figure enter image description here

例如,indexPath 1.4.3.2指定的路径如图 enter image description here

Here in your case indexPath.rowreturns the index of the row at the specific indexPath.

在您的情况下,此处indexPath.row返回特定 indexPath.

Differences betweenindexPath.row and indexPath.item

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

Generally indexPathhas two properties

一般indexPath两个属性

1 - row

2 - item

1 -

2 -项目

row -property use with UITableViewfor get specific rowbase on indexPath. it is also read only property

行-财产的使用UITableView为获得特定上indexPath基地。它也是只读属性

 Available in iOS 2.0 and later.
 Available in iOS 2.0 and later.

item -properly use with UICollectionViewfor get itemin section. It is a read-only property. To use this property you need to declare it in
UICollectionView.h

item -正确UICollectionView用于获取部分中的项目。它是只读属性。要使用此属性,您需要在UICollectionView.h 中声明它

>     Available in iOS 6.0 and later.

回答by Midhun MP

You need to use indexPath.row

你需要使用 indexPath.row

Difference is that:

区别在于:

indexPath.row is for tableView and indexPath.item is for collectionView.

indexPath.row 用于 tableView 和 indexPath.item 用于 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

物品

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

讨论

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

Available in iOS 6.0 and later.

在 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.

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

讨论

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

Available in iOS 2.0 and later.

Please check the NSIndexPath Additionsfor details

请检查NSIndexPath附加的细节

回答by spencery2

@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.

@Owen Godfrey 的回答比 @iPatel 接受的回答要好。这里有一些进一步的说明,我无法对他的答案发表评论,因此我将复制他的答案并添加到此处。信用属于欧文。



From @Owen Godfrey:

来自@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.

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

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.

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



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].

NSIndexPath 的核心接口定义在 NSIndexPath.h 中。索引的存储在 _indexes 中,它是 NSUInteger 的私有一维数组。NSIndexPath 本身可以表示任意数量的维度。NSIndexPath 上有两个相关类别可以扩展功能,一个来自 UICollectionView.h“NSIndexPath (UICollectionViewAdditions)”,另一个来自 UITableView.h“NSIndexPath (UITableView)”。UICollectionView.h 中的一个添加了只读属性“item”和相关的便捷方法。UITableView.h 中的一个添加了只读属性“row”和相关的便捷方法。然而,这两个属性都只是访问 _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.

由于 UIKit 与这两个类别都链接,因此无论您在 IOS 中的哪个位置使用它们,这两组便利功能始终可用。所以你可以从 [NSIndexPath indexPathForRow:inSection:] 创建一个 NSIndexPath 但从 indexPath.item 中检索第二个索引。无论是通过 indexPath.item 还是通过 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.

从风格上讲,如果您使用 UICollectionView 的“item”和 UITableView 的“row”,那么它会更清晰,因为这就是它们的使用方式,这使得代码更具可读性。但是,如果您交换它们,您的程序不会崩溃。

Reference: NSIndexPath

参考:NSIndexPath

回答by Alex Zavatone

Look at the bottom of UICollectionView.h and you will see the category that extends NSIndexPath to add itemas a property when used within for UICollectionView instances.

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

There is a similar section at the bottom of UITableView.h which adds rowand sectionproperties for NSIndexPaths that are used in UITableViews.

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

If you are trying to access these properties of an NSIndexPath instance within a class and the NSIndexPathInstance doesn't believe they are there, just import the header of the class that defines them into the top of your class and you will magically be able to access these properties.

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

UICollectionView.h

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

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 within your class you'll have to import the desired one into your class like so:

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

@import "UIKit/UITableView.h"

@import "UIKit/UITableView.h"

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

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