Skip to content

Instantly share code, notes, and snippets.

@Air-Craft
Created April 7, 2015 10:01
Show Gist options
  • Star 0 You must be signed in to star a gist
  • Fork 0 You must be signed in to fork a gist
  • Save Air-Craft/c49038987588bd47cf66 to your computer and use it in GitHub Desktop.
Save Air-Craft/c49038987588bd47cf66 to your computer and use it in GitHub Desktop.
Add/subtract from an IndexPath `item` property overflowing into `section` #objective-c #cocoa #index
- (NSIndexPath *)_indexPathByAdding:(NSInteger)N toIndexPath:(NSIndexPath *)idxPath
{
const NSInteger SIZE = 8;
NSInteger item = idxPath.item + N;
NSInteger sectionDelta = trunc(item / SIZE);
item = abs((int)item) % SIZE;
NSInteger section = idxPath.section + sectionDelta;
// nil when we go too far back
if (section < 0) {
return nil;
}
return [NSIndexPath indexPathForItem:item inSection:section];
}
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment