2012-11-05 18 views
18

सेल को प्रदर्शित करते समय, WillCisplayCell प्रतिनिधि विधि को कॉल करने के लिए UICollectionView में हैक करने के लिए वैसे भी?UICollectionView WillDisplayCell प्रतिनिधि विधि होगा?

मुझे आलसी लोडिंग के लिए इसकी आवश्यकता है, और मैं इसे UITableView के साथ अच्छा कर रहा हूं, लेकिन आधिकारिक तौर पर UICollectionView में उस तरह की प्रतिनिधि विधि नहीं है।

तो, कोई विचार? धन्यवाद!

+4

बस: क्यों dequeueReusableCellWithReuseIdentifier है: forIndexPath: पर्याप्त नहीं? क्या वह पहले से आलसी लोडिंग नहीं है? मैंने सोचा कि सेल इस विधि के ठीक बाद प्रदर्शित होगा। – Masa

+1

@ मासा यह "आलसी लोडिंग" की तरह है, लेकिन कल्पना करें कि आपके पास 2208x1242 पिक्सल पर एक पूर्णस्क्रीन छवि थी और आप केवल एक दृश्यमान छवि को एक http कनेक्शन की अनुमति देना चाहते थे। 'cellForItemAtIndexPath' कई बार आग लग जाएगा, यह मूल रूप से prefetching है, आलसी लोडिंग नहीं। एफवाईआई: यह प्रतिनिधि आईओएस 8 https://developer.apple.com/library/IOs/documentation/UIKit/Reference/UICollectionViewDelegate_protocol/index.html#//apple_ref/occ/intfm/UICollectionViewDelegate/collectionView:willDisplayCell:forItemAtIndexPath में उपलब्ध है: –

उत्तर

10

FYI करें, यह iOS 8 के लिए एपीआई में जोड़ा गया है:

जिज्ञासा से
- (void)collectionView:(UICollectionView *)collectionView willDisplayCell:(UICollectionViewCell *)cell forItemAtIndexPath:(NSIndexPath *)indexPath NS_AVAILABLE_IOS(8_0); 
+3

आधिकारिक दस्तावेज बेहद भ्रमित है। देखें कि वे चर्चा के तहत क्या कहते हैं 'चर्चा संग्रह दृश्य अपनी सामग्री में सेल जोड़ने से पहले इस विधि को कॉल करता है। कोशिका परिवर्धन का पता लगाने के लिए इस विधि का प्रयोग करें, क्योंकि यह दिखाई देने पर सेल की निगरानी करने के विरोध में है। रुको, क्या? तो यह WillDisplay पर अधिसूचित नहीं है? –

+0

इसका उपयोग आईओएस 8+ पर प्रदर्शित होने से पहले एक सेल सेट अप करने के लिए किया जा सकता है। इसे आईओएस 7 और पिछले पर नहीं कहा जाएगा। – buildsucceeded

0

मैं (आलसी लोड हो रहा है छवियों के लिए) cellForItemAtIndexPath: में कुछ इस तरह कर रहा हूँ:

  • अपने मॉडल छवि है, तो बस यह
  • करने के लिए सेल की छवि सेट मॉडल एक नहीं है, तो छवि, एसिंक लोड
  • जब लोड पूर्ण हो जाता है, तो चेक करें कि मूल इंडेक्सपैथ अभी भी
  • मूल इंडेक्सपैथ के लिए cellForItemAtIndexPath पर कॉल करें। चूंकि छवि अब मॉडल में मौजूद है, इसलिए सेल की छवि अब सेट हो जाएगी।

इस तरह लग रहा है:

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

    PhotoCell * cell = [collectionView dequeueReusableCellWithReuseIdentifier:@"PhotoCell" forIndexPath:indexPath]; 
id imageItem = [self.photoSet objectAtIndex:indexPath.row]; 
     ImageManager * listingImage = (ImageManager *)imageItem; 
     if(listingImage.image){ 
      cell.image = listingImage.image; 
     } else { 
      [listingImage loadImage:^(UIImage *image) { 
       [collectionView reloadItemsAtIndexPaths:@[indexPath]]; 
       dispatch_async(dispatch_get_main_queue(), ^{ 
        if ([[collectionView indexPathsForVisibleItems] containsObject:indexPath]) { 
         [(PhotoCell *)[collectionView cellForItemAtIndexPath:indexPath] setImage:image]; 
        } 
       }); 
      }]; 
     } 
    } 

    return cell; 
} 
0

आप आलसी लोड करने के लिए SDWebImagehttps://github.com/rs/SDWebImage का उपयोग कर सकते। आप इसे UICollectionView के साथ प्रभावी ढंग से उपयोग कर सकते हैं।


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

/*--------- 
----Other CollectiveView stuffs------ 
-----------------*/ 
if([[NSFileManager defaultManager] fileExistsAtPath:YOUR_FILE_PATH isDirectory:NO]) 
{ 
     imagView.image=[UIImage imageWithContentsOfFile:YOUR_FILE_PATH]; 
} 
else 
{ 
    UIActivityIndicatorView *act=[[UIActivityIndicatorView alloc]initWithActivityIndicatorStyle:UIActivityIndicatorViewStyleGray]; 
    [imagView addSubview:act]; 
    act.center=CGPointMake(imagView.frame.size.width/2, imagView.frame.size.height/2); 
    [act startAnimating]; 

    __weak typeof(UIImageView) *weakImgView = imagView; 
    [imagView setImageWithURL:[NSURL URLWithString:REMOTE_FILE_PATH] completed:^(UIImage *image, NSError *error, SDImageCacheType cacheType){ 
    for(UIView *dd in weakImgView.subviews) 
    { 
     if([dd isKindOfClass:[UIActivityIndicatorView class]]) 
     { 
      UIActivityIndicatorView *act=(UIActivityIndicatorView *)dd; 
      [act stopAnimating]; 
      [act removeFromSuperview]; 
     } 
    } 
    NSString *extension=[YOUR_FILE_PATH pathExtension]; 

    [self saveImage:image withFileName:YOUR_FILE_PATH ofType:extension]; 
    }]; 
} 
} 
0

मैं जब मैं सेल के सूचकांक पथ जो प्रदर्शित किया जा रहा है पता करने की जरूरत एक ऐसी ही स्थिति है। - (void)collectionView:(UICollectionView *)collectionView didEndDisplayingCell:(UICollectionViewCell *)cell forItemAtIndexPath:(NSIndexPath *)indexPath के साथ समाप्त हुआ। संभवतः, एक "didEndDisplaying" है, दूसरा "विलुप्त" होगा।

+1

दरअसल, यह बिल्कुल अलग है। स्क्रीन पर दिखाई देने के लिए सेल को प्रस्तुत करने से पहले WillDisplayCell ठीक हो जाएगा। didEndDisplayingCell स्क्रीन के लिए सेल गायब हो जाने के ठीक बाद ट्रिगर होता है। यह कहना है कि यदि आप didDndplayingCell का उपयोग करते हैं, तो आप अंतिम सेल पर पता लगाकर एक अनंत स्क्रॉल नहीं कर सकते, क्योंकि उपयोगकर्ता को इसे प्रदर्शित करना होगा, और उन्हें नए तत्वों को लोड करने के क्रम में गायब होने के लिए बस इतना ही स्क्रॉल करें। वास्तव में उपयोगकर्ता के अनुकूल नहीं है, है ना? – CyberDandy

+0

मैंने यह पता लगाने के तरीकों पर स्विच किया कि UIScrollView नीचे – BabyPanda

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