How to increase NSIndexPath

I have a data situation where I want to use an index path. When I view the data, I want to increase the last node NSIndexPath. The code I have so far is:

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

This is a bit like, uncomfortable. Is there a better way to do this?

+6
source share
3 answers

You can try this - perhaps just as awkwardly, but at least a little shorter:

 NSInteger newLast = [indexPath indexAtPosition:indexPath.length-1]+1; indexPath = [[indexPath indexPathByRemovingLastIndex] indexPathByAddingIndex:newLast]; 
+6
source

One line less:

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

+5
source

Check out my solution on Swift:

 func incrementIndexPath(indexPath: NSIndexPath) -> NSIndexPath? { var nextIndexPath: NSIndexPath? let rowCount = numberOfRowsInSection(indexPath.section) let nextRow = indexPath.row + 1 let currentSection = indexPath.section if nextRow < rowCount { nextIndexPath = NSIndexPath(forRow: nextRow, inSection: currentSection) } else { let nextSection = currentSection + 1 if nextSection < numberOfSections { nextIndexPath = NSIndexPath(forRow: 0, inSection: nextSection) } } return nextIndexPath } 
+2
source

Source: https://habr.com/ru/post/910352/


All Articles