代码之家  ›  专栏  ›  技术社区  ›  Ronnie Liew

如何创建nsindexxpath:indexPathWithIndexes:length所需的“索引”:

  •  41
  • Ronnie Liew  · 技术社区  · 16 年前

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

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

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

    文件将其列为 组成索引路径的索引数组 但它需要一个(nsinteger*)。

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

    4 回复  |  直到 9 年前
        1
  •  61
  •   Quinn Taylor    13 年前

    你说得对。您可以这样使用它:

    NSUInteger indexArr[] = {1,2,3,4};
    
    NSIndexPath *indexPath = [NSIndexPath indexPathWithIndexes:indexArr length:4];
    
        2
  •  44
  •   Quinn Taylor    13 年前

    在iOS上,您还可以从 NSIndexPath UIKit Additions (在uiTableView.h中声明):

    + (NSIndexPath*) indexPathForRow:(NSUInteger)row inSection:(NSUInteger)section
    
        3
  •  7
  •   Giao    16 年前

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

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

        4
  •  4
  •   jungledev    9 年前

    我用了两行代码

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

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