如何创建NSIndexPath所需的"索引":indexPathWithIndexes:length:

Ron*_*iew 42 cocoa objective-c

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

+ (id)indexPathWithIndexes:(NSUInteger *)indexes length:(NSUInteger)length
Run Code Online (Sandbox Code Playgroud)

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

文档将其列为构成索引路径的索引数组,但它期望(NSUinteger*).

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

Bar*_*ark 61

你是对的.您可以像这样使用它:

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

NSIndexPath *indexPath = [NSIndexPath indexPathWithIndexes:indexArr length:4];
Run Code Online (Sandbox Code Playgroud)

  • @Steve不是这种情况.您可以在Cocoa的内存管理编程指南中找到所有与Cocoa相关的内存管理问题的答案(http://developer.apple.com/mac/library/documentation/cocoa/Conceptual/MemoryMgmt/MemoryMgmt.html) .因为`indexPathWithIndexes:length`在选择器中没有'new','copy'或'alloc',所以Cocoa规则指定它返回一个自动释放的实例. (4认同)

小智 44

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

+ (NSIndexPath*) indexPathForRow:(NSUInteger)row inSection:(NSUInteger)section
Run Code Online (Sandbox Code Playgroud)

  • 上面的方法与下面的代码相同:`NSUInteger indexes [] = {section,row}; [NSIndexPath indexPathWithIndexes:indices length:2];`(如果您同时定位iOS和Mac平台,则NSIndexPath UIKit扩展不可用,因此您需要使用此方法.) (3认同)
  • 它有很好的文档记录:http://developer.apple.com/iphone/library/documentation/uikit/reference/NSIndexPath_UIKitAdditions/Reference/Reference.html,但它只能在iphone(UIKit)上使用. (2认同)

Gia*_*iao 7

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

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