如何增加NSIndexPath

dre*_*kka 6 objective-c nsindexpath ios

我有一个数据情况,我想使用索引路径.当我遍历数据时,我想增加NSIndexPath的最后一个节点.我到目前为止的代码是:

int nbrIndex = [indexPath length];
NSUInteger *indexArray = (NSUInteger *)calloc(sizeof(NSUInteger),nbrIndex);
[indexPath getIndexes:indexArray];
indexArray[nbrIndex - 1]++;
[indexPath release];
indexPath = [[NSIndexPath alloc] initWithIndexes:indexArray length:nbrIndex];
free(indexArray);
Run Code Online (Sandbox Code Playgroud)

这感觉有点,嗯,笨重 - 有更好的方法吗?

das*_*ght 6

你可以尝试这个 - 也许同样笨重,但至少有点短:

NSInteger newLast = [indexPath indexAtPosition:indexPath.length-1]+1;
indexPath = [[indexPath indexPathByRemovingLastIndex] indexPathByAddingIndex:newLast];
Run Code Online (Sandbox Code Playgroud)


Rob*_*way 5

少行一行:

indexPath = [NSIndexPath indexPathForRow:indexPath.row+1 inSection:actualIndexPath.section];