SectionIndexTitles for a UICollectionView
I had a similar requirement (for a horizontal collection view) and ended up building an index view subclass myself.
I plan to open-source it but that will likely have to wait until next month, so here's a stub to get you started:
YMCollectionIndexView.h
@interface YMCollectionIndexView : UIControl
- (id) initWithFrame:(CGRect)frame indexTitles:(NSArray *)indexTitles;
// Model
@property (strong, nonatomic) NSArray *indexTitles; // NSString
@property (readonly, nonatomic) NSUInteger currentIndex;
- (NSString *)currentIndexTitle;
@end
YMCollectionIndexView.m
#import "YMCollectionIndexView.h"
@interface YMCollectionIndexView ()
@property (readwrite, nonatomic) NSUInteger currentIndex;
@property (strong, nonatomic) NSArray *indexLabels;
@end
@implementation YMCollectionIndexView
- (id) initWithFrame:(CGRect)frame indexTitles:(NSArray *)indexTitles {
self = [super initWithFrame:frame];
if (self) {
self.indexTitles = indexTitles;
self.currentIndex = 0;
// add pan recognizer
}
return self;
}
- (void)setIndexTitles:(NSArray *)indexTitles {
if (_indexTitles == indexTitles) return;
_indexTitles = indexTitles;
[self.indexLabels makeObjectsPerformSelector:@selector(removeFromSuperview)];
[self buildIndexLabels];
}
- (NSString *)currentIndexTitle {
return self.indexTitles[self.currentIndex];
}
#pragma mark - Subviews
- (void) buildIndexLabels {
CGFloat cumulativeItemWidth = 0.0; // or height in your (vertical) case
for (NSString *indexTitle in self.indexTitles) {
// build and add label
// add tap recognizer
}
self.indexLabels = indexLabels;
}
#pragma mark - Gestures
- (void) handleTap:(UITapGestureRecognizer*)recognizer {
NSString *indexTitle = ((UILabel *)recognizer.view).text;
self.currentIndex = [self.indexTitles indexOfObject:indexTitle];
[self sendActionsForControlEvents:UIControlEventTouchUpInside];
}
// similarly for pan recognizer
@end
In your view controller:
- (void)viewDidLoad {
[super viewDidLoad];
[self.collectionIndexView addTarget:self action:@selector(indexWasTapped:) forControlEvents:UIControlEventTouchUpInside];
// similarly for pan recognizer
}
- (void)indexWasTapped:(id)sender {
[self.collectionView scrollToIndexPath:...];
}
// similarly for pan recognizer
How to add indexTitles in a collection view? (iOS 10+)
If you look at UICollectionView.h
, indexTitlesForCollectionView
is only available in tvos:
/// Returns a list of index titles to display in the index view (e.g. ["A", "B", "C" ... "Z", "#"])
- (nullable NSArray<NSString *> *)indexTitlesForCollectionView:(UICollectionView *)collectionView API_AVAILABLE(tvos(10.2));
You might have read Apple's documentation, which states that indexTitle methods are available for iOS 10.3+ and tvOS 10.2+: https://developer.apple.com/documentation/uikit/uicollectionviewdatasource/2851455-indextitles
I tried implementing it on iOS and it just doesn't work, which leads me to believe that somehow they made a mistake in the documentation.
Swift - Custom SectionIndexTitles not working in Tableview
In the UIControl, the subviews UserInteractionEnabled had to be set to false.
Related Topics
iPad Multitasking Support Requires These Orientations
Differences Between Websockets and Long Polling for Turn Based Game Server
Presentviewcontroller and Displaying Navigation Bar
Color in Storyboard Not Matching Uicolor
Uicollectionview Performance - _Updatevisiblecellsnow
How Do iOS Push Notifications Work
Converting a Vision Vntextobservation to a String
How to Pretty Print Swift Dictionaries to the Console
Xcode/Simulator: How to Run Older iOS Version
How to Animate Add Uisearchbar on Top of Uinavigationbar
Uitableviewcell Separator Disappearing in iOS7
Differencebetween Embedded Binaries and Linked Frameworks
Launch Screen Storyboard Not Displaying Image
Scale Text Label by Screen Size
How to Change Navigation Bar Color in iOS 7 or 6