Skip to content

Instantly share code, notes, and snippets.

@plantpurecode
Created January 3, 2012 22:56
Show Gist options
  • Save plantpurecode/1557411 to your computer and use it in GitHub Desktop.
Save plantpurecode/1557411 to your computer and use it in GitHub Desktop.
A handy category on NSIndexPath.
@implementation NSIndexPath (JRAdditions)
- (NSIndexPath *) indexPathByAddingRows:(NSInteger)rows andSections:(NSInteger)sections {
NSIndexPath *newIndexPath = [NSIndexPath indexPathForRow:MIN(NSIntegerMax, [self row] + rows)
inSection:MIN(NSIntegerMax, [self section] + sections)];
return newIndexPath;
}
- (NSIndexPath *) indexPathByAddingRows:(NSInteger) rows {
return [self indexPathByAddingRows:rows andSections:0];
}
- (NSIndexPath *) indexPathByAddingSections:(NSInteger) sections {
return [self indexPathByAddingRows:0 andSections:sections];
}
- (NSIndexPath *) indexPathByRemovingRows:(NSInteger)rows andSections:(NSInteger)sections {
NSIndexPath *newIndexPath = [NSIndexPath indexPathForRow:MAX(0, [self row] - rows)
inSection:MAX(0, [self section] - sections)];
return newIndexPath;
}
- (NSIndexPath *) indexPathByRemovingRows:(NSInteger) rows {
return [self indexPathByRemovingRows:rows andSections:0];
}
- (NSIndexPath *) indexPathByRemovingSections:(NSInteger) sections {
return [self indexPathByRemovingRows:0 andSections:sections];
}
@end
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment