Skip to content

Instantly share code, notes, and snippets.

@kreeger
Created November 13, 2012 03:09
Show Gist options
  • Save kreeger/4063724 to your computer and use it in GitHub Desktop.
Save kreeger/4063724 to your computer and use it in GitHub Desktop.
A UITableView category for traversing index paths.
#import <UIKit/UIKit.h>
@interface UITableView (IndexPathHelpers)
- (NSIndexPath *)minimumIndexPath;
- (NSIndexPath *)maximumIndexPath;
- (NSIndexPath *)incrementIndexPath:(NSIndexPath *)indexPath;
- (NSIndexPath *)decrementIndexPath:(NSIndexPath *)indexPath;
@end
@implementation UITableView (IndexPathHelpers)
- (NSIndexPath *)minimumIndexPath {
return [NSIndexPath indexPathForRow:0 inSection:0];
}
- (NSIndexPath *)maximumIndexPath {
NSInteger lastSection = self.numberOfSections - 1;
NSInteger lastRow = [self numberOfRowsInSection:lastSection] - 1;
return [NSIndexPath indexPathForRow:lastRow inSection:lastSection];
}
- (NSIndexPath *)incrementIndexPath:(NSIndexPath *)indexPath {
if ([indexPath compare:self.maximumIndexPath] == NSOrderedSame) return nil;
NSInteger lastRow = [self numberOfRowsInSection:indexPath.section] - 1;
if (indexPath.row == lastRow) return [NSIndexPath indexPathForRow:0 inSection:(indexPath.section + 1)];
else return [NSIndexPath indexPathForRow:(indexPath.row + 1) inSection:indexPath.section];
}
- (NSIndexPath *)decrementIndexPath:(NSIndexPath *)indexPath {
if ([indexPath compare:self.minimumIndexPath] == NSOrderedSame) return nil;
if (indexPath.row == 0) {
NSInteger lastRow = [self numberOfRowsInSection:(indexPath.section - 1)];
return [NSIndexPath indexPathForRow:lastRow inSection:(indexPath.section - 1)];
} else return [NSIndexPath indexPathForRow:(indexPath.row - 1) inSection:indexPath.section];
}
@end
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment