2014-03-30 57 views
1

在數據獲得分析後,Im很難使UICollectionView圖像加載。UICollectionView僅在Scroll上加載圖像

這是蘋果LazyTable的解決方案,以及對萬物工作的tableview但

的CollectionView圖片只有當我滾動apear。其他數據如標籤或其他apears

沒有問題的單元格圖像給我一個麻煩。

這是我代表裏面重載方法:

__block ParseOperation *weakParser = parser; 

    parser.completionBlock = ^(void) { 
     if (weakParser.appRecordList) { 
      dispatch_async(dispatch_get_main_queue(), ^{ 
       CategroyScreenViewController *rootViewController = (CategroyScreenViewController*)[(UINavigationController*)self.window.rootViewController topViewController]; 

       rootViewController.entries = weakParser.appRecordList; 
       [rootViewController.collectionView reloadData]; 
      }); 
     } 
     self.queue = nil; 
    }; 

    [self.queue addOperation:parser]; 
    self.appListData = nil; 
} 

,這是我CategroyScreenViewController包含的CollectionView:

#define kCustomRowCount  7 

@interface CategroyScreemViewController() <UIScrollViewDelegate> 
@property (nonatomic, strong) NSMutableDictionary *imageDownloadsInProgress; 
@property (nonatomic) NSMutableArray* numbers; 
@end 
int num = 0; 
@implementation CategroyScreemViewController 

- (id)initWithNibName:(NSString *)nibNameOrNil bundle:(NSBundle *)nibBundleOrNil 
{ 
    self = [super initWithNibName:nibNameOrNil bundle:nibBundleOrNil]; 
    if (self) { 
     // Custom initialization 
    } 
    return self; 
} 

- (void)viewDidLoad 
{ 
    NSLog(@"view did load"); 
    [super viewDidLoad]; 
    self.imageDownloadsInProgress = [NSMutableDictionary dictionary]; 
    [self.collectionView reloadData]; 


} 

- (CGSize) blockSizeForItemAtIndexPath:(NSIndexPath *)indexPath { 


    NSLog(@"Asking for index paths of non-existant cells!! %d", indexPath.row); 

    if (indexPath.row == 0) return CGSizeMake(2, 2);  
    return CGSizeMake(1, 1); 
} 
- (void) viewDidAppear:(BOOL)animated { 
    NSLog(@"viewDidAppear load"); 
    [self.collectionView reloadData]; 
} 
- (UIEdgeInsets)insetsForItemAtIndexPath:(NSIndexPath *)indexPath { 
     NSLog(@"insetsForItemAtIndexPath load"); 
    return UIEdgeInsetsMake(2, 2, 2, 2); 
} 
- (void)didReceiveMemoryWarning 
{ 
    [super didReceiveMemoryWarning]; 
    NSArray *allDownloads = [self.imageDownloadsInProgress allValues]; 
    [allDownloads makeObjectsPerformSelector:@selector(cancelDownload)]; 

    [self.imageDownloadsInProgress removeAllObjects]; 
} 
-(NSInteger)numberOfSectionsInCollectionView:(UICollectionView *)collectionView 
{ 
    NSLog(@"numberOfSectionsInCollectionView load"); 
    return 1; 
} 

-(NSInteger)collectionView:(UICollectionView *)collectionView numberOfItemsInSection:(NSInteger)section 
{ 
    NSUInteger count = [self.entries count]; 
     NSLog(@"numberOfItemsInSection load"); 
    if (count == 0) 
    { 
     return kCustomRowCount; 
    } 
    return count; 
} 
-(UICollectionViewCell *)collectionView:(UICollectionView *)collectionView cellForItemAtIndexPath:(NSIndexPath *)indexPath 
{ 
    CategoryScreenCell *myCell = [collectionView dequeueReusableCellWithReuseIdentifier:@"MyCell" forIndexPath:indexPath]; 
    UILabel *label = (UILabel *)[myCell.contentView viewWithTag:10]; 
    NSUInteger nodeCount = [self.entries count]; 
    if (nodeCount > 0) 
    { 
     AppRecord *appRecord = [self.entries objectAtIndex:indexPath.row]; 
     [label setText:appRecord.appName]; 
     if (!appRecord.appIcon) 
     { 

      if (self.collectionView.dragging == NO && self.collectionView.decelerating == NO) 
      { 

       [self startIconDownload:appRecord forIndexPath:indexPath]; 
      } 
      myCell.imageView.image = [UIImage imageNamed:@"Placeholder.png"]; 
     } 
     else 
     { 

      myCell.imageView.image = appRecord.appIcon; 
     } 

    } 

    return myCell; 
} 
- (UIColor*) colorForNumber:(NSNumber*)num { 
    return [UIColor colorWithHue:((19 * num.intValue) % 255)/255.f saturation:1.f brightness:1.f alpha:1.f]; 
} 
- (void)startIconDownload:(AppRecord *)appRecord forIndexPath:(NSIndexPath *)indexPath 
{ 
    IconDownloader *iconDownloader = [self.imageDownloadsInProgress objectForKey:indexPath]; 
    if (iconDownloader == nil) 
    { 
     iconDownloader = [[IconDownloader alloc] init]; 
     iconDownloader.appRecord = appRecord; 
     [iconDownloader setCompletionHandler:^{ 

      CategoryScreenCell *myCell = [self.collectionView dequeueReusableCellWithReuseIdentifier:@"MyCell" forIndexPath:indexPath]; 
      myCell.imageView.image = appRecord.appIcon; 
      myCell.imageView.layer.masksToBounds = YES; 
      [self.imageDownloadsInProgress removeObjectForKey:indexPath]; 

     }]; 
     [self.imageDownloadsInProgress setObject:iconDownloader forKey:indexPath]; 
     [iconDownloader startDownload]; 
    } 
} 
- (void)loadImagesForOnscreenRows 
{ 
    NSLog(@"loadImagesForOnscreenRows"); 
    if ([self.entries count] > 0) 
    { 
     NSArray *visiblePaths = [self.collectionView indexPathsForVisibleItems]; 
     for (NSIndexPath *indexPath in visiblePaths) 
     { 
      AppRecord *appRecord = [self.entries objectAtIndex:indexPath.item]; 

      if (!appRecord.appIcon) 
       // Avoid the app icon download if the app already has an icon 
      { 
       [self startIconDownload:appRecord forIndexPath:indexPath]; 
      } 
     } 
    } 
} 

- (void)scrollViewDidEndDragging:(UIScrollView *)scrollView willDecelerate:(BOOL)decelerate 
{ 
    NSLog(@"scrollViewDidEndDragging"); 

    if (!decelerate) 
    { 
     NSLog(@"decelerate"); 
     [self loadImagesForOnscreenRows]; 
    } 
} 


- (void)scrollViewDidEndDecelerating:(UIScrollView *)scrollView 
{ 
    NSLog(@"scrollViewDidEndDecelerating"); 
    [self loadImagesForOnscreenRows]; 
} 

@end 

確定後,香港專業教育學院改變了這一點:

 CategoryScreenCell *myCell = [self.collectionView dequeueReusableCellWithReuseIdentifier:@"MyCell" forIndexPath:indexPath]; 

到這一個:

CategoryScreenCell *myCell = [self.collectionView cellForItemAtIndexPath:indexPath]; 

程序運作良好,但它給了我這樣的警告: 「不兼容的指針類型初始化‘CategoryScreenCell *’類型的表達式 ‘UICollectionViewCell *’

回答

1

我用這種方法看到的問題是,在您IconDownloader的完成處理程序,你總是出隊的單元和填充它,即使它不可見並可能不會顯示。我不確定這是多少開銷 - 這取決於您擁有多少個單元以及人們可能滾動的速度。

一種替代方法是在完成處理程序中發佈通知,所有單元格將註冊該通知。在通知發生時調用的方法中,單元格將檢查通知的userInfo中的AppRecord是否適用於當前單元格,並將更新其圖像。

另一個問題是,您爲每個滾動的單元格排列圖像下載,因此如果用戶快速滾動到集合視圖的底部,則必須等待上面的所有圖像先加載。您可以通過幾種方法解決此問題:

1)當您將其出隊時,請給單元格參考IconDownloader。然後在單元的prepareForReuse中,如果仍處於掛起狀態,請取消下載。 2)如果你不想取消掛起的下載,但想要確保儘可能快地更新可見的單元格,你可以把你的下載放在一個優先級隊列中,並增加每個新的優先級IconDownloader實例。

+0

非常感謝你的方法幫助很多 –

1

你可以只是投放擺脫警告細胞類型,假設你知道這將是需要的類型:

CategoryScreenCell *myCell = (CategoryScreenCell *)[self.collectionView cellForItemAtIndexPath:indexPath]; 
+0

謝謝你做到了。 –

相關問題