2013-02-14 47 views
10

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

यहाँ मेरी स्थिर डेटा है कि उन्हें भरता है:

self.menuItems = @[@{@"text" : @"First", @"image" : @"180-stickynote.png"}, 
        @{@"text" : @"Second", @"image" : @"180-stickynote.png"}, 
        @{@"text" : @"Third", @"image" : @"180-stickynote.png"}, 
        @{@"text" : @"Fourth", @"image" : @"180-stickynote.png"}, 
        @{@"text" : @"Fifth", @"image" : @"180-stickynote.png"}, 
        @{@"text" : @"Sixth", @"image" : @"180-stickynote.png"}]; 

और सेल प्रदाता, जहां कस्टम उपवर्ग सिर्फ एक UILabel और एक UIImageView प्रोटोटाइप सेल से जुड़ी और भी है:

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

    CUMenuCollectionViewCell *cell = [collectionView dequeueReusableCellWithReuseIdentifier:@"MenuCell" forIndexPath:indexPath]; 

    NSDictionary *cellInfo = [self.menuItems objectAtIndex:indexPath.row]; 

    cell.imageView.image = [UIImage imageNamed:[cellInfo valueForKey:@"image"]]; 

    cell.label.text = [cellInfo valueForKey:@"text"]; 

    return cell; 
} 

यहां चुनी गई पंक्ति विधि, शीर्षक लॉगिंग, और आइटम की पंक्ति (वे सभी अनुभाग 0 में हैं):

- (void)collectionView:(UICollectionView *)collectionView didDeselectItemAtIndexPath:(NSIndexPath *)indexPath 
{ 
    NSLog(@"%@: %d", [[self.menuItems objectAtIndex:indexPath.row] valueForKey:@"text"], indexPath.row); 
} 

अन्त में, मेरे मेनू के स्क्रीनशॉट:

enter image description here

यहाँ लॉग जब मैं पहली से आइटम का चयन करने के लिए है छठी, फिर वापस छठी से करने के लिए सबसे पहले (पहले, दूसरे, तीसरे, चौथे, पांचवां, छठा, छठे, पांचवां, चौथा, तीसरा, दूसरा, पहले) (12 कुल नल, ध्यान दें कि पहली नल भी रजिस्टर नहीं है, और न ही दूसरा छठी नल):

------------------------------------- FIRST TAP ON FIRST HERE 
2013-02-13 19:38:37.343 App[1383:c07] First: 0 // second tap, on Second 
2013-02-13 19:38:38.095 App[1383:c07] Second: 1 // third tap, on Third 
2013-02-13 19:38:38.678 App[1383:c07] Third: 2 // fourth tap, on Fourth 
2013-02-13 19:38:39.375 App[1383:c07] Fourth: 3 // fifth tap, on Fifth 
2013-02-13 19:38:40.167 App[1383:c07] Fifth: 4 // so on 
2013-02-13 19:38:41.751 App[1383:c07] Sixth: 5 
------------------------------------- SECOND TAP ON SIXTH HERE 
2013-02-13 19:38:42.654 App[1383:c07] Fifth: 4 
2013-02-13 19:38:43.318 App[1383:c07] Fourth: 3 
2013-02-13 19:38:44.495 App[1383:c07] Third: 2 
2013-02-13 19:38:45.071 App[1383:c07] Second: 1 

उत्तर

52

ऐसा इसलिए है क्योंकि तुम हो didSelectItemAtIndexPath: के बजाय didDeselectItemAtIndexPath: विधि का उपयोग कर रहे हैं। बनाने के लिए एक आसान गलती, विशेष रूप से यदि आप इसे टाइप करते समय कोड पूर्णता का उपयोग कर रहे हैं।

+1

वाह। मैं थोड़ा सा चिल्ला सकता हूं, अभी बहुत देर हो चुकी है। ईमानदारी से, बहुत बहुत धन्यवाद! – Josh

+0

@ जोश, कोई समस्या नहीं, यह एक गलती है जिसे मैंने एक से अधिक बार बनाया है। – rdelmar

+0

वाह। यह गलती भी बनाई। सोचा कि काम पर कुछ अंधेरा जादूगर था। लेकिन नहीं। खुद को बड़ा समय बेवकूफ बना दिया :) – villapossu