How to create the “indexes” required for NSIndexPath:indexPathWithIndexes:length:

前端 未结 4 722
鱼传尺愫
鱼传尺愫 2020-12-23 16:45

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

+ (id)indexPathWithIndexes:(NSUInteger *)indexes length:(NSUInteger)length
相关标签:
4条回答
  • 2020-12-23 17:01

    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.

    0 讨论(0)
  • 2020-12-23 17:07

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

    + (NSIndexPath*) indexPathForRow:(NSUInteger)row inSection:(NSUInteger)section
    
    0 讨论(0)
  • 2020-12-23 17:12

    You are correct. You might use it like this:

    NSUInteger indexArr[] = {1,2,3,4};
    
    NSIndexPath *indexPath = [NSIndexPath indexPathWithIndexes:indexArr length:4];
    
    0 讨论(0)
  • 2020-12-23 17:16

    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.

    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).

    0 讨论(0)
提交回复
热议问题