有人知道 NSIndexpath.row
和 NSIndexpath.item
之间的区别吗?
具体来说,我在哪一个中使用:
-(UITableViewCell *)tableView:(UITableView *)tableView cellForRowAtIndexPath:(NSIndexPath *)indexPath;
row
被 UITableView
行使用; item
由 UICollectionView
单元格使用。
好吧,这里没有人给出好的答案。
在 NSIndexPath 中,索引存储在一个名为“_indexes”的简单 c 数组中,定义为 NSUInteger*,数组的长度存储在定义为 NSUInteger 的“_length”中。访问器“section”是“_indexes[0]”的别名,“item”和“row”都是“_indexes[1]”的别名。因此两者在功能上是相同的。
就编程风格(也许还有定义链)而言,您最好在表的上下文中使用“行”,在集合的上下文中使用“项目”。
indexPath.row is best in your case
关于 NSIndexPath 的第一条信息
NSIndexPath
类表示嵌套数组集合树中特定节点的路径。此路径称为索引路径。
indexPath 中的每个索引都表示从树中的一个节点到另一个更深节点的子数组的索引。
https://i.stack.imgur.com/YaXYY.png
在您的情况下,indexPath.row
返回特定 indexPath
处的行索引。
之间的差异 indexPath.row and indexPath.item
通常 indexPath
有两个属性
1 - 第 2 行 - 项目
row - 属性与 UITableView
一起使用,以获取基于 indexPath 的特定 row。它也是只读属性
在 iOS 2.0 及更高版本中可用。
item - 与 UICollectionView
一起正确使用以获取部分中的 item。它是一个只读属性。要使用此属性,您需要在
UICollectionView.h 中声明它
> Available in iOS 6.0 and later.
您需要使用 indexPath.row
区别在于:
indexPath.row 用于 tableView,indexPath.item 用于 collectionView。
item 标识集合视图部分中的项目的索引号。 (只读)@property(非原子,只读)NSInteger 项;讨论 项目所在的部分由部分的值标识。可用性 适用于 iOS 6.0 及更高版本。在 UICollectionView.h 中声明
行 标识表视图部分中的行的索引号。 (只读) @property(nonatomic, readonly) NSInteger 行;讨论 行所在的部分由 section 的值标识。可用性 适用于 iOS 2.0 及更高版本。
@Owen Godfrey 的回答比@iPatel 接受的回答要好。这里有一些进一步的说明,我无法对他的回答发表评论,所以我将复制他的回答并在此处添加。信用属于欧文。
来自@Owen Godfrey:
在 NSIndexPath 中,索引存储在一个名为“_indexes”的简单 c 数组中,定义为 NSUInteger*,数组的长度存储在定义为 NSUInteger 的“_length”中。访问器“section”是“_indexes[0]”的别名,“item”和“row”都是“_indexes1”的别名。因此两者在功能上是相同的。
就编程风格(也许还有定义链)而言,您最好在表的上下文中使用“行”,在集合的上下文中使用“项目”。
NSIndexPath 的核心接口在 NSIndexPath.h 中定义。索引的存储在 _indexes 中,它是 NSUInteger 的私有一维数组。 NSIndexPath 本身可以表示任意数量的维度。 NSIndexPath 上有两个相关的类别来扩展功能,一个来自 UICollectionView.h “NSIndexPath (UICollectionViewAdditions)”,另一个来自 UITableView.h “NSIndexPath (UITableView)”。 UICollectionView.h 中的一个添加了只读属性“item”和相关的便利方法。 UITableView.h 中的一个添加了只读属性“row”和相关的便利方法。然而,这两个属性都只是访问 _indexes[1] 中基础值的包装器。
由于 UIKit 与这两个类别都链接,因此无论您在 IOS 的哪个位置使用它们,这两组便利功能始终可用。因此,您可以从 [NSIndexPath indexPathForRow:inSection:] 创建一个 NSIndexPath,但从 indexPath.item 检索第二个索引。无论是通过 indexPath.item 还是 indexPath.row 访问,基础值都完全相同。
从风格上讲,如果您将“item”与 UICollectionView 一起使用,而将“row”与 UITableView 一起使用,则会更简洁,因为这就是它们的用途,这使得代码更具可读性。但是,如果您交换它们,您的程序不会崩溃。
参考:NSIndexPath
查看 UICollectionView.h 的底部,您将看到扩展 NSIndexPath 以将 item
作为属性添加到 UICollectionView 实例中的类别。
UITableView.h 底部有一个类似的部分,它为 UITableViews 中使用的 NSIndexPaths 添加了 row
和 section
属性。
如果您尝试访问类中的 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]
不定期副业成功案例分享
NSIndexPath
的更广泛用途。