2013-06-05 17 views
5

में हैंडलिंग टैप जेस्चर है क्योंकि मैं एक फोटो एलबम बनाने के लिए किसी भी ढांचे का उपयोग नहीं कर सका, मैं संग्रह दृश्य का उपयोग करके अपना खुद का निर्माण करने की कोशिश कर रहा हूं, लेकिन मैं शुरुआत में ठीक हो गया।UICollectionView

मेरा लक्ष्य मेरी वेब सेवा से सभी छवियों को मेरे संग्रह दृश्य में प्रदर्शित करना है, क्योंकि सभी प्रदर्शित होते हैं, अगला चरण तब होता है जब कोई किसी भी सेल पर टैप करता है, मैं इसे एक नए दृश्य में खोल सकता हूं और सभी के बीच नेविगेट भी कर सकता हूं।

- (void)viewDidLoad 
{ 
    [super viewDidLoad]; 
    // Do any additional setup after loading the view. 
    [collectionController reloadData]; 
    tapGesture = [[UITapGestureRecognizer alloc] initWithTarget:nil action:@selector(touched)]; 

    tapGesture.numberOfTapsRequired = 1; 


} 

- (void)didReceiveMemoryWarning 
{ 
    [super didReceiveMemoryWarning]; 
    // Dispose of any resources that can be recreated. 
} 

-(NSInteger)numberOfSectionsInCollectionView:(UICollectionView *)collectionView{ 

    return 1; 

} 

-(NSInteger)collectionView:(UICollectionView *)collectionView numberOfItemsInSection:(NSInteger)section{ 

    return 6; 
} 

-(UICollectionViewCell *)collectionView:(UICollectionView *)collectionView cellForItemAtIndexPath:(NSIndexPath *)indexPath{ 

    static NSString *cellIdentifier = @"Cell"; 

    CollectionCell *cell = [collectionView dequeueReusableCellWithReuseIdentifier:cellIdentifier forIndexPath:indexPath]; 
    [cell.imgCollection setImageWithURL:[NSURL URLWithString:@"http://sallescds.com.br/wp-content/uploads/2012/12/xepop-300x300.jpg"] placeholderImage:[UIImage imageNamed:@"placeholder.png"]]; 

    [cell.imgCollection addGestureRecognizer:tapGesture]; 

    return cell; 
} 

-(void)touched:(UIGestureRecognizer *)tap{ 

    NSLog(@"the touch happened"); 
} 

धन्यवाद लोग:

यहाँ बुनियादी कोड है कि मैं बनाया है।

उत्तर

14

योग्य कुछ बातें अपने कोड में सही नहीं हैं:

पहले, initWithTarget:action: लक्ष्य के लिए एक nil मूल्य पारित नहीं किया जाना चाहिए। the docs से:

लक्ष्य

एक वस्तु रिसीवर जब यह एक जेस्चर को पहचान के द्वारा भेजे गए संदेशों के कार्रवाई प्राप्तकर्ता है। शून्य मान्य मान नहीं है।

अपने मामले में आप एक लक्ष्य के रूप self पारित क्योंकि आप अपने वर्ग की वर्तमान उदाहरण के लिए संदेश touched: भेजा करना चाहते हैं चाहिए।

दूसरा, आप जिस चयनकर्ता को initWithTarget:action: पर पास कर चुके हैं वह गलत है। आपने @selector(touched) का उपयोग किया है लेकिन आपकी विधि कार्यान्वयन - (void)touched:(UIGestureRecognizer *)tap; है, जो चयनकर्ता @selector(touched:) है (: पर दिमाग)।

यदि आप उलझन में हैं तो this question on selectors पढ़ने की सलाह देंगे।

तीसरा, आप कई (this SO question देखें) को देखने के लिए एक एकल UIGestureRecognizer संलग्न नहीं कर सकते हैं।

तो इसे काम करने के लिए, आप एक संग्रह सेल (शायद एक सबक्लास में) UITapGestureRecognizer बना सकते हैं। या बेहतर अभी तक, अपने UICollectionViewDelegate विधि collectionView:didSelectItemAtIndexPath: लागू करें।

संपादित करें - collectionView:didSelectItemAtIndexPath: लागू करना सीखें:

- (void)viewDidLoad 
{ 
    [super viewDidLoad]; 
    // Do any additional setup after loading the view. 

    // Bind the collectionView's delegate to your view controller 
    // This could also be set without code, in your storyboard 
    self.collectionView.delegate = self; 
} 

-(NSInteger)numberOfSectionsInCollectionView:(UICollectionView *)collectionView 
{ 
    return 1; 
} 

-(NSInteger)collectionView:(UICollectionView *)collectionView numberOfItemsInSection:(NSInteger)section 
{ 
    return 6; 
} 

-(UICollectionViewCell *)collectionView:(UICollectionView *)collectionView cellForItemAtIndexPath:(NSIndexPath *)indexPath{ 

    static NSString *cellIdentifier = @"Cell"; 

    UICollectionViewCell *cell = [collectionView dequeueReusableCellWithReuseIdentifier:cellIdentifier forIndexPath:indexPath]; 
    [cell.imgCollection setImageWithURL:[NSURL URLWithString:@"http://sallescds.com.br/wp-content/uploads/2012/12/xepop-300x300.jpg"] placeholderImage:[UIImage imageNamed:@"placeholder.png"]]; 

    return cell; 
} 

// I implemented didSelectItemAtIndexPath:, but you could use willSelectItemAtIndexPath: depending on what you intend to do. See the docs of these two methods for the differences. 
- (void)collectionView:(UICollectionView *)collectionView didSelectItemAtIndexPath:(NSIndexPath *)indexPath 
{ 
    // If you need to use the touched cell, you can retrieve it like so 
    UICollectionViewCell *cell = [collectionView cellForItemAtIndexPath:indexPath]; 

    NSLog(@"touched cell %@ at indexPath %@", cell, indexPath); 
} 
+0

समझ लिया, मैं पहले से ही इन समस्याओं, तय मेरी गलती है जब मैं अंग्रेज़ी के लिए मेरे तरीकों के नाम का अनुवाद किया गया था। मदद के लिए धन्यवाद, अब मुझे इसे समझना है, UICollectionViewDelegate में ऐसा कैसे करें, क्योंकि मुझे कोई जानकारी नहीं है। क्या आप मुझे एक उदाहरण या ऐसा कुछ दे सकते हैं? –

+0

ठीक है, इसलिए मुझे इस विधि को कॉल करना होगा - (BOOL) संग्रह दृश्य: (UICollectionView *) संग्रह दृश्य देखना चाहिए ItemAtIndexPath: (NSIndexPath *) indexPath; और इसके अंदर इशारा बनाओ? –

+2

यह वास्तव में बहुत अधिक सरल है। यदि आप प्रतिनिधियों के तरीकों को लागू करते हैं, तो आपको इशारा पहचानकर्ताओं की आवश्यकता नहीं है। एक स्पर्श पता चला है जब प्रतिनिधि अपने तरीकों के कार्यान्वयन को बुलाएगा। ध्यान दें कि आपको स्पष्ट रूप से 'चाहिए चयन करें' या 'चयन किया गया' कॉल करने की आवश्यकता नहीं है, प्रतिनिधि यह आपके लिए करता है। –