2012-11-01 6 views
9

मैं एक मौजूदा UICollectionView है, जो पहले से ही कुछ कोशिकाओं से भर जाता है के लिए कुछ और कोशिकाओं को जोड़ने के लिए कोशिश कर रहा हूँ करने के लिए और अधिक UICollectionViewCell जोड़ें।एक मौजूदा UICollectionView

मैंने संग्रह दृश्य reloadData का उपयोग करने का प्रयास किया लेकिन ऐसा लगता है कि पूरे संग्रह दृश्य को फिर से लोड किया गया है और मैं बस और सेल्स जोड़ना चाहता था।

क्या कोई मेरी मदद कर सकता है?

उत्तर

6

UICollectionView वर्ग को जोड़ने/आइटम हटाने के लिए तरीकों है। जैसे, कुछ index पर एक आइटम डालने के लिए (खंड 0 में), उसके अनुसार अपनी मॉडल को संशोधित करें और कार्य करें:

int indexPath = [NSIndexPath indexPathForItem:index]; 
NSArray *indexPaths = [NSArray arrayWithObject:indexPath inSection:0]; 
[collectionView insertItemsAtIndexPaths:indexPaths]; 

दृश्य आराम करेंगे।

+0

तो, जब मैं पहली बार उपयोग '[UICollectionView reloadData]' मैं उपयोग किए गए अंतिम सेल के सूचकांक को बचाने चाहिए, और फिर, जब मैं और अधिक कोशिकाओं को जोड़ने के लिए विधि कॉल मैं, सही अपनी नवीनतम सेल indexPath का उपयोग करना चाहिए ? @chris –

+0

आइटम जोड़ने के दौरान आपको '[UICollectionView reloadData]' की आवश्यकता नहीं है। बस 'insertItemsAtIndexPaths कार्य करें:' और यह सुनिश्चित करें कि आपके डेटा स्रोत के लिए बाद में कॉल 'तरीकों 'collectionView बनाने: numberOfItemsInSection:' और 'collectionView: cellForItemAtIndexPath:' आपके परिवर्तनों के। – chris

+0

मैंने इसे पहले किया था यह काम किया। लेकिन, जब नई कोशिकाओं प्रदर्शन पर शो हो जाएगा मैं संदेश मिलता है: 'न आया हुआ अपवाद 'NSRangeException', कारण की वजह से एप्लिकेशन समाप्त: '- [__ NSCFArray objectAtIndex:]: सूचकांक (36) सीमा से परे (36)' ' @ क्रिस क्या आप जानते हैं कि मैंने क्या गलत किया? –

5

अपने सभी सेल को फिर से लोड करने के लिए बिना UICollectionView को नई कोशिकाओं को सम्मिलित करने के लिए सबसे आसान तरीका performBatchUpdates है, जो नीचे दिए गए चरणों का पालन करके आसानी से किया जा सकता है का उपयोग करना है।

// Lets assume you have some data coming from a NSURLConnection 
[NSURLConnection sendAsynchronousRequest:request queue:[NSOperationQueue mainQueue] completionHandler:^(NSURLResponse *response, NSData *data, NSError *erro) 
{ 
     // Parse the data to Json 
     NSMutableArray *newJson = [NSJSONSerialization JSONObjectWithData:data options:NSJSONReadingMutableContainers error:nil]; 

     // Variable used to say at which position you want to add the cells 
     int index; 

     // If you want to start adding before the previous content, like new Tweets on twitter 
     index = 0; 

     // If you want to start adding after the previous content, like reading older tweets on twitter 
     index = self.json.count; 

     // Create the indexes with a loop 
     NSMutableArray *indexes = [NSMutableArray array]; 

     for (int i = index; i < json.count; i++) 
     { 
      [indexes addObject:[NSIndexPath indexPathForItem:i inSection:0]]; 
     } 

     // Perform the updates 
     [self.collectionView performBatchUpdates:^{ 

      //Insert the new data to your current data 
      [self.json addObjectsFromArray:newJson]; 

      //Inser the new cells 
      [self.collectionView insertItemsAtIndexPaths:indexes]; 

     } completion:nil]; 
} 
+0

यह विधि पिछली कोशिकाओं को गायब करती है फिर फिर लोड करें और संग्रह स्क्रॉल करें शीर्ष पर देखें। –

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

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