gpt4 book ai didi

ios - UICollectionView 的 SectionIndexTitles

转载 作者:IT王子 更新时间:2023-10-29 08:03:48 25 4
gpt4 key购买 nike

所以我实现了一个具有搜索功能和 sectionIndexTitles 的适当 TableView。现在,我正在尝试实现一个 UICollectionView,到目前为止它一直在工作,只是我不能轻易地拥有 sectionIndexTitles(右侧滚动条)。

如果我查看 Facebook 应用程序,它看起来像一个 UICollectionView,但确实有 sectionIndexTitles 和一个搜索栏。我似乎无法为 UICollectionView 模型找到这样的函数。

有什么想法吗?!

谢谢!

enter image description here

最佳答案

我有类似的要求(对于水平 Collection View )并最终自己构建了一个索引 View 子类。

我计划将其开源,但可能要等到下个月,所以这里有一个帮助您入门的 stub :

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

在您的 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

关于ios - UICollectionView 的 SectionIndexTitles,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/13882052/

25 4 0
Copyright 2021 - 2024 cfsdn All Rights Reserved 蜀ICP备2022000587号
广告合作:1813099741@qq.com 6ren.com