UICollectionView的SectionIndexTitles

所以我实现了一个适当的TableView与searchfunction和sectionIndexTitles 。 现在,我正在尝试实现一个UICollectionView ,它工作到目前为止,除了我不能很容易地有sectionIndexTitles (右滚动条)。

如果我看Facebook应用程序,它看起来像一个UICollectionView ,但确实sectionIndexTitles和一个searchUICollectionView 。 我似乎无法findUICollectionView模型的这种function。

有任何想法吗?!

谢谢!

在这里输入图像说明

我有一个类似的需求(水平集合视图),并最终build立一个索引视图子类我自己。

我打算开源,但可能要等到下个月,所以这里有一个存根:

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 

在你的视图控制器中:

 - (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 

你有没有find解决办法?

我试图为UICollectionView实现相同的function。

到目前为止sectionIndexTitles我使用自定义UITableView与原始UICollectionView相同的部分,但与空单元格。 因此,我有一个IndexTableView类

 indexTableView = [[IndexTableView alloc] initWithFrame:CGRectZero]; [self.view addSubview:indexTableView]; ... indexTableView.frame = CGRectMake(CGRectGetWidth(self.view.frame) - 40, 0, 40, CGRectGetHeight(self.view.frame)); indexTableView.separatorStyle = UITableViewCellSeparatorStyleNone; indexTableView.sectionHeaderHeight = 0; indexTableView.backgroundColor = [UIColor clearColor]; 

我在UICollectionView的顶部添加了IndexTableView,并获得了与原始UITableView相同的视觉效果。 现在我试图链接索引select和UICollectionView并使其工作。

编辑:我刚刚添加一个UISearchBar到UICollectionController

将以下代码添加到UICollectionController的子类中

 - (void)viewDidLoad { [super viewDidLoad]; ... self.searchBar = [[UISearchBar alloc] initWithFrame:CGRectMake(0, 0, self.collectionView.frame.size.width, 44)]; self.searchBar.autocorrectionType = UITextAutocorrectionTypeNo; self.searchBar.delegate = self; [self.collectionView addSubview:self.searchBar]; [self.collectionView setContentOffset:CGPointMake(44, 0)]; } - (UIEdgeInsets)collectionView:(UICollectionView *)collectionView layout:(UICollectionViewLayout*)collectionViewLayout insetForSectionAtIndex:(NSInteger)section { if (section == 0) { return UIEdgeInsetsMake(CGRectGetHeight(self.searchBar.frame), 6, 0, 6); } return UIEdgeInsetsMake(0, 6, 0, 6); } 

你现在可以简单地使用(从iOS 10.3开始)

optional func indexTitles(for collectionView: UICollectionView) -> [String]?

其中需要一个数组: ['A', 'B' ,'C']作为它的返回值。

您可以通过为UICollectionViewDataSource实现并返回以下函数的值来提供此标头

 collectionView:viewForSupplementaryElementOfKind:atIndexPath: 

请注意,标题的大小将从UICollectionViewFlowLayoutDelegate方法collectionView返回的CGRect的高度值:layout:referenceSizeForHeaderInSection: