Created
April 7, 2015 10:01
-
-
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
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
- (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