Warning: file_get_contents(/data/phpspider/zhask/data//catemap/9/ios/120.json): failed to open stream: No such file or directory in /data/phpspider/zhask/libs/function.php on line 167

Warning: Invalid argument supplied for foreach() in /data/phpspider/zhask/libs/tag.function.php on line 1116

Notice: Undefined index: in /data/phpspider/zhask/libs/function.php on line 180

Warning: array_chunk() expects parameter 1 to be array, null given in /data/phpspider/zhask/libs/function.php on line 181
Ios UICollectionView仅在滚动时加载图像_Ios_Objective C_Uicollectionview - Fatal编程技术网

Ios UICollectionView仅在滚动时加载图像

Ios UICollectionView仅在滚动时加载图像,ios,objective-c,uicollectionview,Ios,Objective C,Uicollectionview,我很难在数据解析后加载UICollectionView图像 这是苹果公司提供的一个懒散的解决方案,任何东西都可以在tableview上工作,但对于 CollectionView图像仅在我滚动时显示。其他数据,如标签或其他标签 手机没有问题,只有图像给我带来麻烦 我的委托中有一个重载方法: __block ParseOperation *weakParser = parser; parser.completionBlock = ^(void) { if (weak

我很难在数据解析后加载UICollectionView图像

这是苹果公司提供的一个懒散的解决方案,任何东西都可以在tableview上工作,但对于

CollectionView图像仅在我滚动时显示。其他数据,如标签或其他标签

手机没有问题,只有图像给我带来麻烦

我的委托中有一个重载方法:

    __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;
}
这是我的CategroyScreenView控制器,包含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 cellForItemAtIndexPath:indexPath];
程序运行良好,但它给了我以下警告: 不兼容的指针类型正在使用类型为的表达式初始化“CategoryScreenCell*”
“UICollectionViewCell*”

您可以通过强制转换单元格类型来消除警告,前提是您知道它将是所需的类型:

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

我看到这种方法的问题是,在你的
IconDownloader
的完成处理程序中,你总是将一个单元格出列并填充它,即使它不可见也可能不显示。我不确定这会有多大的开销——这取决于你有多少单元格以及人们滚动的速度

一种替代方法是在完成处理程序中发布通知,所有单元格都将注册该通知。在通知发生时调用的方法中,单元格将检查通知的
userInfo
中的
AppRecord
是否用于当前单元格,并更新其映像

另一个问题是,您为滚动经过的每个单元格排队下载图像,因此如果用户快速滚动到集合视图的底部,他们必须等待上面的所有图像首先加载。您可以通过以下几种方法解决此问题:

1) 退出队列时,为单元格提供对
IconDownloader
的引用。然后在单元格的
prepareForReuse
中,如果仍挂起,请取消下载

2) 如果您不想取消挂起的下载,但想确保可见单元格尽快更新,可以将下载放入优先级队列,并增加每个新的
IconDownloader
实例的优先级

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