objective-c 如何创建 NSIndexPath:indexPathWithIndexes:length 所需的“索引”:

声明:本页面是StackOverFlow热门问题的中英对照翻译,遵循CC BY-SA 4.0协议,如果您需要使用它,必须同样遵循CC BY-SA许可,注明原文地址和作者信息,同时你必须将它归于原作者(不是我):StackOverFlow 原文地址: http://stackoverflow.com/questions/181432/
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-09-03 20:48:44  来源:igfitidea点击:

How to create the "indexes" required for NSIndexPath:indexPathWithIndexes:length:

objective-ccocoa

提问by Ronnie Liew

The class method to create an index path with one or more nodes is:

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

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

How do we create the "indexes" required in the first parameter?

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

The documentation listed it as Array of indexes to make up the index pathbut it is expecting a (NSUinteger *).

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

To create an index path of 1.2.3.4, is it simply an array of [1,2,3,4] ?

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

回答by Barry Wark

You are correct. You might use it like this:

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

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

NSIndexPath *indexPath = [NSIndexPath indexPathWithIndexes:indexArr length:4];

回答by Barry Wark

On iOS, you can also use this method from NSIndexPath UIKit Additions(declared in UITableView.h):

在 iOS 上,你也可以使用NSIndexPath UIKit Additions 中的这个方法(在 UITableView.h 中声明):

+ (NSIndexPath*) indexPathForRow:(NSUInteger)row inSection:(NSUInteger)section

回答by Giao

You assumption is correct. It's as simple as a C array of NSUInteger. The length parameter is the number of elements in the indexes array.

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

Arrays in C are often identified as a pointer (in this case NSUInteger *) with a length parameter or a known terminator such as \0 for C strings (which is just a char array).

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

回答by jungledev

I did this in 2 lines of code

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

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

Short, clean, and readable. Free code, don't knock it.

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