42

创建具有一个或多个节点的索引路径的类方法是:

+ (id)indexPathWithIndexes:(NSUInteger *)indexes length:(NSUInteger)length

我们如何创建第一个参数中所需的“索引”?

文档将其列为索引数组以构成索引路径,但它需要一个 (NSUinteger *)。

要创建 1.2.3.4 的索引路径,是否只是 [1,2,3,4] 的数组?

4

4 回答 4

61

你是对的。你可以这样使用它:

NSUInteger indexArr[] = {1,2,3,4};

NSIndexPath *indexPath = [NSIndexPath indexPathWithIndexes:indexArr length:4];
于 2008-10-08T19:32:12.827 回答
44

在 iOS 上,您还可以使用NSIndexPath UIKit Additions中的此方法(在 UITableView.h 中声明):

+ (NSIndexPath*) indexPathForRow:(NSUInteger)row inSection:(NSUInteger)section
于 2009-05-06T11:31:11.860 回答
7

你的假设是正确的。它就像 NSUInteger 的 C 数组一样简单。length 参数是索引数组中元素的数量。

C 中的数组通常被标识为带有长度参数的指针(在本例中为 NSUInteger *)或已知的终止符,例如 C 字符串的 \0(它只是一个 char 数组)。

于 2008-10-08T05:14:21.763 回答
4

我用两行代码做到了这一点

 NSMutableArray *indexPaths = [[NSMutableArray alloc] init];   
 for (int i = firstIndexYouWant; i < totalIndexPathsYouWant; i++) [indexPaths addObject:[NSIndexPath indexPathForRow:i inSection:0]];

简短、干净、易读。免费代码,不要敲它。

于 2015-09-10T20:28:23.700 回答