2012-12-14 15 views
20

के लिए सेक्शनइंडेक्स टाइटल इसलिए मैंने एक खोज कार्यक्षमता और sectionIndexTitles के साथ उचित TableView लागू किया। अब, मैं UICollectionView को कार्यान्वित करने की कोशिश कर रहा हूं और यह अब तक काम कर रहा है, सिवाय इसके कि मुझे आसानी से sectionIndexTitles (दायां स्क्रॉल बार) नहीं मिल सकता है।एक UICollectionView

यदि मैं फेसबुक एप्लिकेशन को देखता हूं, तो यह UICollectionView जैसा दिखता है, लेकिन वास्तव में sectionIndexTitles और एक खोजबार के साथ। मुझे UICollectionView मॉडल के लिए ऐसे फ़ंक्शन नहीं मिल रहे हैं।

कोई विचार ?!

धन्यवाद!

enter image description here

+0

मुझे भी इस पर एक अच्छा जवाब चाहिए; यदि संभव हो तो मैं एक और टेबल दृश्य छिपाने से बचना चाहूंगा (स्मृति समस्याएं)। –

उत्तर

15

के अपने उपवर्ग को UICollectionController

निम्न कोड जोड़ें मैं एक ऐसी ही आवश्यकता (एक क्षैतिज संग्रह के लिए किया था करने के लिए एक UISearchBar जोड़ दिया है देखें) और एक सूचकांक दृश्य subclass खुद का निर्माण समाप्त हो गया।

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:

मैं खुले स्रोत के लिए यह योजना लेकिन संभावना है कि अगले महीने तक इंतजार करना होगा, इसलिए यहां एक ठूंठ पाने के लिए आप शुरू कर दिया है। मीटर

#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 

आपके विचार ग में ontroller:

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

यह शानदार लग रहा है! यदि आप इसे ओपन-सोर्स करते हैं, तो आईओएस समुदाय बड़े पैमाने पर आपको इसके लिए प्यार करेगा, मुझे यकीन है। बाउंटी से सम्मानित! –

+3

मैंने [यहां एक गिस्ट) में एक पूर्ण कार्यान्वयन (लेखन के साथ) को दबाया है (https://gist.github.com/kreeger/4756030) - मुझे बताएं कि आप क्या सोचते हैं। –

+0

ऐसा लगता है कि @ बेनक्रिगर ने अपने समाधान के लिए गिटहब रेपो/कोकोपॉड बनाया: https://github.com/kreeger/BDKCollectionIndexView –

-2

आप को लागू करने और के लिए UICollectionViewDataSource

collectionView:viewForSupplementaryElementOfKind:atIndexPath: 

नोट निम्नलिखित समारोह के लिए एक मूल्य लौटने कि शीर्ष लेख के आकार CGRect की ऊंचाई मूल्य से किया जाएगा लौट आने तक इस हेडर प्रदान कर सकते हैं UICollectionViewFlowLayoutDelegate विधि संग्रह दृश्य: लेआउट: संदर्भ आकारफोरहेडर इनसेक्शन:

+1

प्रश्न स्क्रीन के दाईं ओर 'sectionIndexTitles' को संदर्भित करता है, न कि पूरक अनुभाग शीर्षलेख। –

+0

आप सही हैं, मुझे इस पर सवाल याद आया। – Samuel

0

क्या आपको कोई समाधान मिला है?

मैं UICollectionView के लिए समान सुविधाओं को लागू करने की कोशिश कर रहा हूं।

अब तक अनुभाग इंडेक्सटाइटल के लिए मैं मूल UICableView का उपयोग मूल 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 रूप में एक ही दृश्य प्रभाव मिला है। अब मैं इंडेक्स चयन और यूआईसीओलेक्शन व्यू को जोड़ने और इसे काम करने की कोशिश कर रहा हूं।

संपादित: मैं सिर्फ 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); 
} 
+0

क्या यह काम करता है ?! UICollectionController के लिए – abisson

+0

UISearchBar काम करता है। IndexTableView पूरी तरह से नहीं, मैंने अभी तक इसे समाप्त नहीं किया है। – Eugene

0

अब आप बस आईओएस 10 के रूप में (उपयोग कर सकते हैं।3)

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

कौन सा की तरह एक सरणी की उम्मीद: ['A', 'B' ,'C'] अपनी वापसी मान के रूप में ।

+1

यह किसी के लिए यह काम करता है? यहां तक ​​कि एक साधारण उदाहरण में भी मुझे इंडेक्स टाइटल फ्लेक नहीं मिल सकता है। –

+0

यह भी समझने के लिए उत्सुक है कि इस नए डेटा स्रोत विधि का उपयोग कैसे करें। मैंने आईओएस 10.3 और टीवीओएस 10.2 (जैसा कि दस्तावेज़ों में उल्लिखित है) पर दोनों की कोशिश की है लेकिन इंडेक्स टाइटल को कभी नहीं दिखाया गया है। –

+0

मैं इसे आईओएस पर काम करने के लिए नहीं मिला (अभी तक टीवीओएस का प्रयास नहीं किया)। ऑनलाइन दस्तावेज बताता है कि यह आईओएस 10.3+ और टीवीओएस 10.2+ पर उपलब्ध है, लेकिन हेडर फाइलों को देखकर, यह केवल API_AVAILABLE (tvos (10.2)) कहता है। – cargath

 संबंधित मुद्दे

  • कोई संबंधित समस्या नहीं^_^