Warning: file_get_contents(/data/phpspider/zhask/data//catemap/9/ios/118.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_Iphone_Uiscrollview - Fatal编程技术网

Ios 向已运行的UICollectionView添加更多图像

Ios 向已运行的UICollectionView添加更多图像,ios,objective-c,iphone,uiscrollview,Ios,Objective C,Iphone,Uiscrollview,我在viewDidLoad中设置了一个UICollectionView,然后立即添加图像。当用户滚动到某一点时,我会尝试添加更多图像,但集合视图不会加载新图像 如何在scrollview中重新加载或添加更多图像 #pragma mark - New Retrieve - (void) rwDataToPlist { // Step1: Get plist file path NSArray *sysPaths = NSSearchPathForDirectoriesInDom

我在viewDidLoad中设置了一个UICollectionView,然后立即添加图像。当用户滚动到某一点时,我会尝试添加更多图像,但集合视图不会加载新图像

如何在scrollview中重新加载或添加更多图像

#pragma mark - New Retrieve
- (void) rwDataToPlist {

    // Step1: Get plist file path

    NSArray *sysPaths = NSSearchPathForDirectoriesInDomains(NSDocumentDirectory ,NSUserDomainMask, YES);

    NSString *documentsDirectory = [sysPaths objectAtIndex:0];

    NSString *filePath = [documentsDirectory stringByAppendingPathComponent:@"news.plist"];

    NSLog(@"Plist File Path: %@", filePath);

    if ([[NSFileManager defaultManager] fileExistsAtPath:filePath]) {

         stuff = [[NSArray alloc] initWithContentsOfFile:filePath];
        //NSLog(@"Stuff %@", stuff);
        [self sortRetrievedData];
     } else {


     }


}

-(void)sortRetrievedData {
    titles = [stuff valueForKey:kKeyTitle];
    thumbMediaUrl = [stuff valueForKey:kKeyThumbUrl];
    thumbWidth = [stuff valueForKey:kKeyThumbWidth];
    thumbHeight = [stuff valueForKey:kKeyThumbHeight];

    for(NSArray *array in titles) {
        for (NSArray *realArray in array) {
            titles = realArray;
        }
    }
    for(NSArray *array in thumbMediaUrl) {
        for(NSDictionary *dict in array) {
            thumbMediaUrl = dict;
        }
    }
    for(NSArray *array in thumbWidth) {
        for(NSDictionary *dict in array) {
            thumbWidth = dict;
        }
    }
    for(NSArray *array in thumbHeight) {
        for(NSDictionary *dict in array) {
            thumbHeight = dict;
        }
    }
}

#pragma mark - Collection View
#pragma mark - Display Collection 
-(void)displayCollection {
    UIColor *myColor = [UIColor colorWithRed:(245.0 / 255.0) green:(245.0 / 255.0) blue:(245.0 / 255.0) alpha: 1];

    CGRect screenBound = [[UIScreen mainScreen] bounds];
    CGSize screenSize = screenBound.size;
    CGFloat screenWidth = screenSize.width;
    CGFloat screenHeight = screenSize.height;
    self.view = [[UIView alloc] initWithFrame:[[UIScreen mainScreen] bounds]];
    CGRect locOfScree = CGRectMake(0, 64, screenWidth, screenHeight - 44);

    UICollectionViewFlowLayout *layout=[[UICollectionViewFlowLayout alloc] init];
    imageDisplayer = [[UICollectionView alloc] initWithFrame:locOfScree collectionViewLayout:layout];
    [imageDisplayer setDataSource:self];
    [imageDisplayer setDelegate:self];

    UINib *nib = [UINib nibWithNibName:@"CollectionViewCell" bundle:nil];
    [imageDisplayer registerNib:nib forCellWithReuseIdentifier:CellIdentifier];
    [imageDisplayer setBackgroundColor:myColor];

    [self.view insertSubview:imageDisplayer belowSubview:navBar];
    //[self.view addSubview:imageDisplayer];
}

#pragma mark - Setup

- (void)setupData
{
    self.imageURLStrings = [NSMutableArray array];

    // Save image to docs directory so we can get a nice URL
    // Not sure how to get the URL from an asset catalog off the top of my head

    NSArray *paths = NSSearchPathForDirectoriesInDomains(NSDocumentDirectory, NSUserDomainMask, YES);
    NSString *documentsDirectory = [paths objectAtIndex:0];
    NSString *path = [documentsDirectory stringByAppendingPathComponent:@"greyBurger.png"];

    UIImage *image = [UIImage imageNamed:@"greyBurger"];
    NSData *data = UIImagePNGRepresentation(image);
    [data writeToFile:path atomically:YES];

    // Populate our imageURLStrings array with many paths

    for (NSInteger i = 0; i < NumberOfImages; i++)
    {
        [self.imageURLStrings addObject:path];
        //NSLog(@"Path %@", path);
    }
}

- (void)setupCollectionView
{
    UINib *nib = [UINib nibWithNibName:@"CollectionViewCell" bundle:nil];
    [self.collectionView registerNib:nib forCellWithReuseIdentifier:CellIdentifier];
}

#pragma mark - UICollectionView Datasource

- (NSInteger)collectionView:(UICollectionView *)collectionView numberOfItemsInSection:(NSInteger)section
{
    //return [self.imageURLStrings count];
    return [titles count];
    //return 2;

}



- (UICollectionViewCell *)collectionView:(UICollectionView *)collectionView cellForItemAtIndexPath:(NSIndexPath *)indexPath
{
    CollectionViewCell *cell = (CollectionViewCell *)[collectionView dequeueReusableCellWithReuseIdentifier:CellIdentifier forIndexPath:indexPath];


    //NSLog(@"CELL FOR COUNT:%lu", [titles count]);
    NSString *title = [titles objectAtIndex:indexPath.row];
    cell.backgroundColor = [UIColor whiteColor];
    NSString *imageUrl = [thumbMediaUrl objectForKey:title];
    NSURL *thumbUrl = [NSURL URLWithString:imageUrl];
    UIImageView *imageView = [[UIImageView alloc] init];
    NSData *imageData = [NSData dataWithContentsOfURL:thumbUrl];
    imageView.image = [UIImage imageWithData:imageData];

    cell.backgroundView = imageView;
    cell.layer.shouldRasterize = YES;
    cell.layer.rasterizationScale = [UIScreen mainScreen].scale;
    //cell.frame = CGRectMake(0, 0, 100, 100);

    //cell.imageURLString = [self.imageURLStrings objectAtIndex:indexPath.item];

    return cell;
}

- (CGSize)collectionView:(UICollectionView *)collectionView layout:(UICollectionViewLayout*)collectionViewLayout sizeForItemAtIndexPath:(NSIndexPath *)indexPath
{
    CGRect screenBound = [[UIScreen mainScreen] bounds];
    CGSize screenSize = screenBound.size;
    CGFloat screenWidth = screenSize.width;

    NSString *title = [titles objectAtIndex:indexPath.row];
    float width = [[thumbWidth objectForKey:title] floatValue];
    float height = [[thumbHeight objectForKey:title] floatValue];
    float imageWidth = (screenWidth / 2) - 3;
    float scale = imageWidth / width;
    float imageHeight = height * scale;
    CGSize imageSize = CGSizeMake(imageWidth, imageHeight);


    return imageSize;
}
- (CGFloat)collectionView:(UICollectionView *)collectionView layout:(UICollectionViewLayout*)collectionViewLayout minimumInteritemSpacingForSectionAtIndex:(NSInteger)section {
    return 2.0;
}

- (CGFloat)collectionView:(UICollectionView *)collectionView layout:(UICollectionViewLayout*)collectionViewLayout minimumLineSpacingForSectionAtIndex:(NSInteger)section {
    return 2.0;
}

// Layout: Set Edges
- (UIEdgeInsets)collectionView:
(UICollectionView *)collectionView layout:(UICollectionViewLayout*)collectionViewLayout insetForSectionAtIndex:(NSInteger)section {
    // return UIEdgeInsetsMake(0,8,0,8);  // top, left, bottom, right
    return UIEdgeInsetsMake(0,0,0,0);  // top, left, bottom, right
}

- (NSArray *)layoutAttributesForElementsInRect:(CGRect)rect
{
    NSArray* attributesToReturn = [self layoutAttributesForElementsInRect:rect];
    for (UICollectionViewLayoutAttributes* attributes in attributesToReturn)
    {
        if (nil == attributes.representedElementKind)
        {
            NSIndexPath* indexPath = attributes.indexPath;
            attributes.frame = [self layoutAttributesForItemAtIndexPath:indexPath].frame;
        }
    }
    return attributesToReturn;
}

- (UICollectionViewLayoutAttributes *)layoutAttributesForItemAtIndexPath:(NSIndexPath *)indexPath
{
     UICollectionViewLayoutAttributes* currentItemAttributes = [self layoutAttributesForItemAtIndexPath:indexPath];

    if (indexPath.item < numColumns){
        CGRect f = currentItemAttributes.frame;
        f.origin.y = 0;
        currentItemAttributes.frame = f;
        return currentItemAttributes;
    }
    NSIndexPath* ipPrev = [NSIndexPath indexPathForItem:indexPath.item-numColumns inSection:indexPath.section];
    CGRect fPrev = [self layoutAttributesForItemAtIndexPath:ipPrev].frame;
    CGFloat YPointNew = fPrev.origin.y + fPrev.size.height + 10;
    CGRect f = currentItemAttributes.frame;
    f.origin.y = YPointNew;
    currentItemAttributes.frame = f;
    return currentItemAttributes;
}

一些建议/问题:

  • 由于不知道您的特定问题的限制,听起来您试图显示一些类似于网格的图像,并在用户滚动超过某个点时加载图像的“下一页”。看起来UITableView或UICollectionView是一种更合适的机制来完成您试图做的事情。这两个类都将处理图像的延迟加载、通过正确配置的数据源进行分页、自动调整contentSize,并且将在没有主线程冻结的情况下快速运行。它们也基于MVC设计模式,可能会澄清上面代码中的一些设计

  • 加载更多图像时,UI/滚动冻结的原因可能是从磁盘读取用户默认值和图像数据本身。磁盘操作可能很昂贵,尤其是一次操作很多磁盘。如果您必须以这种方式加载元数据和图像文件,请考虑通过GCD的代码> DeXCHCHYASYNC/<代码>在后台线程上进行此处理。
  • 第二组图像未出现的原因可能是图像帧或scrollView内容大小计算错误。通过在ScLoVIEW中添加一系列有色UIVIEW而不是UIIVIEVIEW来考虑调试,并考虑将Cordsisi.Head硬编码为比您需要的大很多的东西。另外,您是否记录了图像帧和内容大小以确保它们匹配

  • 编辑:

    这里有一个链接,指向一个使用UICollectionView和后台线程从磁盘读取UIImage数据的快速示例项目(由于使用UICollectionView,不再需要该线程):


    如果您有任何疑问,请告诉我,希望我没有完全偏离您的目标。

    随着iOS6本身的启动,苹果为UICollectionView引入了iOS6。您可以使用UICollectionView和下拉式刷新来实现您的目标。它看起来像UtableView拉到底刷新

    以下是UIcollectionView的一些参考:

  • 创建UICollectionView后,需要为UICollectionView添加PulltoBottom刷新,如以下代码所示:

    -(void)viewWillAppear:(BOOL)animated
    {
    
    [self.collectionView.infiniteScrollingView setActivityIndicatorViewStyle:UIActivityIndicatorViewStyleWhite];
    [self.collectionView addInfiniteScrollingWithActionHandler:^{
        ////(@"Infinitescroll");
    
    
    
    
    
    
    
        @try {
            int64_t delayInSeconds = 2.0;
            dispatch_time_t popTime = dispatch_time(DISPATCH_TIME_NOW, delayInSeconds * NSEC_PER_SEC);
            dispatch_after(popTime, dispatch_get_main_queue(), ^(void){
                //[self.dataSource addObjectsFromArray:tmp];
                [self.collectionView performBatchUpdates:^{
    
    
                } completion:nil];
                [self.collectionView.infiniteScrollingView stopAnimating];
            });
        }
        @catch (NSException *exception) {
    
        }
    
    
    
    
    
    }];
    
    [super viewWillAppear:animated];
    }
    
    对于以下代码,您必须添加一些文件:


    我使用了前两个答案中建议的UICollectionView,并添加到其中,以允许我想要的图像间距

    我的代码:

    #pragma mark - Collection View
    #pragma mark - Display Collection 
    -(void)displayCollection {
        UIColor *myColor = [UIColor colorWithRed:(245.0 / 255.0) green:(245.0 / 255.0) blue:(245.0 / 255.0) alpha: 1];
    
        CGRect screenBound = [[UIScreen mainScreen] bounds];
        CGSize screenSize = screenBound.size;
        CGFloat screenWidth = screenSize.width;
        CGFloat screenHeight = screenSize.height;
        self.view = [[UIView alloc] initWithFrame:[[UIScreen mainScreen] bounds]];
        CGRect locOfScree = CGRectMake(0, 44, screenWidth, screenHeight - 44);
    
        CHTCollectionViewWaterfallLayout *layout = [[CHTCollectionViewWaterfallLayout alloc] init];
    
        layout.sectionInset = UIEdgeInsetsMake(2, 2, 2, 2);
        layout.headerHeight = 0;
        layout.footerHeight = 0;
        layout.minimumColumnSpacing = 2;
        layout.minimumInteritemSpacing = 2;
        //UICollectionViewFlowLayout *layout=[[UICollectionViewFlowLayout alloc] init];
        imageDisplayer = [[UICollectionView alloc] initWithFrame:locOfScree collectionViewLayout:layout];
        imageDisplayer.autoresizingMask = UIViewAutoresizingFlexibleHeight | UIViewAutoresizingFlexibleWidth;
    
        [imageDisplayer setDataSource:self];
        [imageDisplayer setDelegate:self];
    
        UINib *nib = [UINib nibWithNibName:@"CollectionViewCell" bundle:nil];
        [imageDisplayer registerNib:nib forCellWithReuseIdentifier:CellIdentifier];
        [imageDisplayer setBackgroundColor:myColor];
    
        [self.view insertSubview:imageDisplayer belowSubview:navBar];
        //[self.view addSubview:imageDisplayer];
    
        imageDisplayer = [[UICollectionView alloc] initWithFrame:self.view.bounds collectionViewLayout:layout];
        imageDisplayer.autoresizingMask = UIViewAutoresizingFlexibleHeight | UIViewAutoresizingFlexibleWidth;
        imageDisplayer.dataSource = self;
        imageDisplayer.delegate = self;
        imageDisplayer.backgroundColor = [UIColor whiteColor];
        [imageDisplayer registerClass:[CHTCollectionViewWaterfallCell class]
            forCellWithReuseIdentifier:CellIdentifier];
    }
    
    #pragma mark - Setup
    - (void)setupCollectionView
    {
        UINib *nib = [UINib nibWithNibName:@"CollectionViewCell" bundle:nil];
        [self.collectionView registerNib:nib forCellWithReuseIdentifier:CellIdentifier];
    }
    
    #pragma mark - UICollectionView Datasource
    
    - (NSInteger)collectionView:(UICollectionView *)collectionView numberOfItemsInSection:(NSInteger)section
    {
        //return [self.imageURLStrings count];
        return [titles count];
        //return 2;
    
    }
    
    
    
    - (UICollectionViewCell *)collectionView:(UICollectionView *)collectionView cellForItemAtIndexPath:(NSIndexPath *)indexPath
    {
        /*
        CollectionViewCell *cell = (CollectionViewCell *)[collectionView dequeueReusableCellWithReuseIdentifier:CellIdentifier forIndexPath:indexPath];
        */
        CHTCollectionViewWaterfallCell *cell =
        (CHTCollectionViewWaterfallCell *)[collectionView dequeueReusableCellWithReuseIdentifier:CellIdentifier
                                                                                    forIndexPath:indexPath];
    
        NSString *title = [titles objectAtIndex:indexPath.row];
        cell.backgroundColor = [UIColor whiteColor];
        NSString *imageUrl = [thumbMediaUrl objectForKey:title];
        NSURL *thumbUrl = [NSURL URLWithString:imageUrl];
        UIImageView *imageView = [[UIImageView alloc] init];
        NSData *imageData = [NSData dataWithContentsOfURL:thumbUrl];
        imageView.image = [UIImage imageWithData:imageData];
    
        cell.backgroundView = imageView;
        cell.layer.shouldRasterize = YES;
        cell.layer.rasterizationScale = [UIScreen mainScreen].scale;
        return cell;
    
    }
    
    - (CGSize)collectionView:(UICollectionView *)collectionView layout:(UICollectionViewLayout*)collectionViewLayout sizeForItemAtIndexPath:(NSIndexPath *)indexPath
    {
        CGRect screenBound = [[UIScreen mainScreen] bounds];
        CGSize screenSize = screenBound.size;
        CGFloat screenWidth = screenSize.width;
    
        NSString *title = [titles objectAtIndex:indexPath.row];
        float width = [[thumbWidth objectForKey:title] floatValue];
        float height = [[thumbHeight objectForKey:title] floatValue];
        float imageWidth = (screenWidth / 2) - 3;
        float scale = imageWidth / width;
        float imageHeight = height * scale;
        CGSize imageSize = CGSizeMake(imageWidth, imageHeight);
    
    
        return imageSize;
    }
    - (CGFloat)collectionView:(UICollectionView *)collectionView layout:(UICollectionViewLayout*)collectionViewLayout minimumInteritemSpacingForSectionAtIndex:(NSInteger)section {
        return 2.0;
    }
    
    - (CGFloat)collectionView:(UICollectionView *)collectionView layout:(UICollectionViewLayout*)collectionViewLayout minimumLineSpacingForSectionAtIndex:(NSInteger)section {
        return 2.0;
    }
    
    // Layout: Set Edges
    - (UIEdgeInsets)collectionView:
    (UICollectionView *)collectionView layout:(UICollectionViewLayout*)collectionViewLayout insetForSectionAtIndex:(NSInteger)section {
        // return UIEdgeInsetsMake(0,8,0,8);  // top, left, bottom, right
        return UIEdgeInsetsMake(0,0,0,0);  // top, left, bottom, right
    }
    
    - (NSArray *)layoutAttributesForElementsInRect:(CGRect)rect
    {
        NSArray* attributesToReturn = [self layoutAttributesForElementsInRect:rect];
        for (UICollectionViewLayoutAttributes* attributes in attributesToReturn)
        {
            if (nil == attributes.representedElementKind)
            {
                NSIndexPath* indexPath = attributes.indexPath;
                attributes.frame = [self layoutAttributesForItemAtIndexPath:indexPath].frame;
            }
        }
        return attributesToReturn;
    }
    
    - (UICollectionViewLayoutAttributes *)layoutAttributesForItemAtIndexPath:(NSIndexPath *)indexPath
    {
         UICollectionViewLayoutAttributes* currentItemAttributes = [self layoutAttributesForItemAtIndexPath:indexPath];
    
        if (indexPath.item < numColumns){
            CGRect f = currentItemAttributes.frame;
            f.origin.y = 0;
            currentItemAttributes.frame = f;
            return currentItemAttributes;
        }
        NSIndexPath* ipPrev = [NSIndexPath indexPathForItem:indexPath.item-numColumns inSection:indexPath.section];
        CGRect fPrev = [self layoutAttributesForItemAtIndexPath:ipPrev].frame;
        CGFloat YPointNew = fPrev.origin.y + fPrev.size.height + 10;
        CGRect f = currentItemAttributes.frame;
        f.origin.y = YPointNew;
        currentItemAttributes.frame = f;
        return currentItemAttributes;
    }
    
    #pragma标记-集合视图
    #pragma标记-显示集合
    -(作废)收款{
    UIColor*myColor=[UIColor color withred:(245.0/255.0)green:(245.0/255.0)blue:(245.0/255.0)alpha:1];
    CGRect屏幕绑定=[[UIScreen mainScreen]边界];
    CGSize screenSize=screensbound.size;
    CGFloat screenWidth=屏幕大小.width;
    CGFloat屏幕高度=屏幕尺寸.height;
    self.view=[[UIView alloc]initWithFrame:[[UIScreen mainScreen]bounds]];
    CGRect LOCOFFSCREE=CGRectMake(0,44,屏幕宽度,屏幕高度-44);
    chtcollectionviewwaterwalllayout*layout=[[chtcollectionviewwaterwalllayout alloc]init];
    layout.sectionInset=UIEdgeInsetsMake(2,2,2,2);
    layout.headerHeight=0;
    layout.footerHeight=0;
    布局。最小列间距=2;
    layout.minimumInteritemSpacing=2;
    //UICollectionViewFlowLayout*布局=[[UICollectionViewFlowLayout alloc]init];
    imageDisplayer=[[UICollectionView alloc]initWithFrame:locofScreeCollectionViewLayout:layout];
    imageDisplayer.autoresizingMask=UIViewAutoresizingFlexibleHeight | UIViewAutoresizingFlexibleWidth;
    [imageDisplayer setDataSource:self];
    [imageDisplayer setDelegate:self];
    UINib*nib=[UINib-nibWithNibName:@“CollectionViewCell”捆绑包:nil];
    [imageDisplayer RegisterInB:nib forCellWithReuseIdentifier:CellIdentifier];
    [图像显示器setBackgroundColor:myColor];
    [self.view insertSubview:imageDisplayer below子视图:导航栏];
    //[self.view addSubview:imageDisplayer];
    imageDisplayer=[[UICollectionView alloc]initWithFrame:self.view.bounds collectionViewLayout:layout];
    imageDisplayer.autoresizingMask=UIViewAutoresizingFlexibleHeight | UIViewAutoresizingFlexibleWidth;
    imageDisplayer.dataSource=self;
    imageDisplayer.delegate=self;
    imageDisplayer.backgroundColor=[UIColor whiteColor];
    [imageDisplayer注册表类:[chtCollectionViewWaterCallCell类]
    forCellWithReuseIdentifier:CellIdentifier];
    }
    #pragma标记-设置
    -(无效)setupCollectionView
    {
    UINib*nib=[UINib-nibWithNibName:@“CollectionViewCell”捆绑包:nil];
    [self.collectionView registerNib:nib forCellWithReuseIdentifier:CellIdentifier];
    }
    #pragma标记-UICollectionView数据源
    -(NSInteger)collectionView:(UICollectionView*)collectionView项目编号截面:(NSInteger)截面
    {
    //返回[self.imageUrlString计数];
    返回[标题计数];
    //返回2;
    }
    -(UICollectionViewCell*)collectionView:(UICollectionView*)collectionView cellForItemAtIndexPath:(NSIndexPath*)indexPath
    {
    /*
    CollectionViewCell*cell=(CollectionViewCell*)[collectionView dequeueReusableCellWithReuseIdentifier:CellIdentifier forIndexPath:indexPath];
    */
    CHTCollectionViewWaterWallCell*单元格=
    (CHTCollectionViewWaterWallCell*)[collectionView dequeueReusableCellWithReuseIdentifier:CellIdentifier
    forIndexPath:indexPath];
    NSString*title=[titles-objectAtIndex:indexath.row];
    cell.backgroundColor=[UIColor whiteColor];
    NSString*imageUrl=[thumbMediaUrl objectForKey:title];
    NSURL*thumbUrl=[NSURL URLWithString:imageUrl];
    UIImageView*ima
    
    #pragma mark - Collection View
    #pragma mark - Display Collection 
    -(void)displayCollection {
        UIColor *myColor = [UIColor colorWithRed:(245.0 / 255.0) green:(245.0 / 255.0) blue:(245.0 / 255.0) alpha: 1];
    
        CGRect screenBound = [[UIScreen mainScreen] bounds];
        CGSize screenSize = screenBound.size;
        CGFloat screenWidth = screenSize.width;
        CGFloat screenHeight = screenSize.height;
        self.view = [[UIView alloc] initWithFrame:[[UIScreen mainScreen] bounds]];
        CGRect locOfScree = CGRectMake(0, 44, screenWidth, screenHeight - 44);
    
        CHTCollectionViewWaterfallLayout *layout = [[CHTCollectionViewWaterfallLayout alloc] init];
    
        layout.sectionInset = UIEdgeInsetsMake(2, 2, 2, 2);
        layout.headerHeight = 0;
        layout.footerHeight = 0;
        layout.minimumColumnSpacing = 2;
        layout.minimumInteritemSpacing = 2;
        //UICollectionViewFlowLayout *layout=[[UICollectionViewFlowLayout alloc] init];
        imageDisplayer = [[UICollectionView alloc] initWithFrame:locOfScree collectionViewLayout:layout];
        imageDisplayer.autoresizingMask = UIViewAutoresizingFlexibleHeight | UIViewAutoresizingFlexibleWidth;
    
        [imageDisplayer setDataSource:self];
        [imageDisplayer setDelegate:self];
    
        UINib *nib = [UINib nibWithNibName:@"CollectionViewCell" bundle:nil];
        [imageDisplayer registerNib:nib forCellWithReuseIdentifier:CellIdentifier];
        [imageDisplayer setBackgroundColor:myColor];
    
        [self.view insertSubview:imageDisplayer belowSubview:navBar];
        //[self.view addSubview:imageDisplayer];
    
        imageDisplayer = [[UICollectionView alloc] initWithFrame:self.view.bounds collectionViewLayout:layout];
        imageDisplayer.autoresizingMask = UIViewAutoresizingFlexibleHeight | UIViewAutoresizingFlexibleWidth;
        imageDisplayer.dataSource = self;
        imageDisplayer.delegate = self;
        imageDisplayer.backgroundColor = [UIColor whiteColor];
        [imageDisplayer registerClass:[CHTCollectionViewWaterfallCell class]
            forCellWithReuseIdentifier:CellIdentifier];
    }
    
    #pragma mark - Setup
    - (void)setupCollectionView
    {
        UINib *nib = [UINib nibWithNibName:@"CollectionViewCell" bundle:nil];
        [self.collectionView registerNib:nib forCellWithReuseIdentifier:CellIdentifier];
    }
    
    #pragma mark - UICollectionView Datasource
    
    - (NSInteger)collectionView:(UICollectionView *)collectionView numberOfItemsInSection:(NSInteger)section
    {
        //return [self.imageURLStrings count];
        return [titles count];
        //return 2;
    
    }
    
    
    
    - (UICollectionViewCell *)collectionView:(UICollectionView *)collectionView cellForItemAtIndexPath:(NSIndexPath *)indexPath
    {
        /*
        CollectionViewCell *cell = (CollectionViewCell *)[collectionView dequeueReusableCellWithReuseIdentifier:CellIdentifier forIndexPath:indexPath];
        */
        CHTCollectionViewWaterfallCell *cell =
        (CHTCollectionViewWaterfallCell *)[collectionView dequeueReusableCellWithReuseIdentifier:CellIdentifier
                                                                                    forIndexPath:indexPath];
    
        NSString *title = [titles objectAtIndex:indexPath.row];
        cell.backgroundColor = [UIColor whiteColor];
        NSString *imageUrl = [thumbMediaUrl objectForKey:title];
        NSURL *thumbUrl = [NSURL URLWithString:imageUrl];
        UIImageView *imageView = [[UIImageView alloc] init];
        NSData *imageData = [NSData dataWithContentsOfURL:thumbUrl];
        imageView.image = [UIImage imageWithData:imageData];
    
        cell.backgroundView = imageView;
        cell.layer.shouldRasterize = YES;
        cell.layer.rasterizationScale = [UIScreen mainScreen].scale;
        return cell;
    
    }
    
    - (CGSize)collectionView:(UICollectionView *)collectionView layout:(UICollectionViewLayout*)collectionViewLayout sizeForItemAtIndexPath:(NSIndexPath *)indexPath
    {
        CGRect screenBound = [[UIScreen mainScreen] bounds];
        CGSize screenSize = screenBound.size;
        CGFloat screenWidth = screenSize.width;
    
        NSString *title = [titles objectAtIndex:indexPath.row];
        float width = [[thumbWidth objectForKey:title] floatValue];
        float height = [[thumbHeight objectForKey:title] floatValue];
        float imageWidth = (screenWidth / 2) - 3;
        float scale = imageWidth / width;
        float imageHeight = height * scale;
        CGSize imageSize = CGSizeMake(imageWidth, imageHeight);
    
    
        return imageSize;
    }
    - (CGFloat)collectionView:(UICollectionView *)collectionView layout:(UICollectionViewLayout*)collectionViewLayout minimumInteritemSpacingForSectionAtIndex:(NSInteger)section {
        return 2.0;
    }
    
    - (CGFloat)collectionView:(UICollectionView *)collectionView layout:(UICollectionViewLayout*)collectionViewLayout minimumLineSpacingForSectionAtIndex:(NSInteger)section {
        return 2.0;
    }
    
    // Layout: Set Edges
    - (UIEdgeInsets)collectionView:
    (UICollectionView *)collectionView layout:(UICollectionViewLayout*)collectionViewLayout insetForSectionAtIndex:(NSInteger)section {
        // return UIEdgeInsetsMake(0,8,0,8);  // top, left, bottom, right
        return UIEdgeInsetsMake(0,0,0,0);  // top, left, bottom, right
    }
    
    - (NSArray *)layoutAttributesForElementsInRect:(CGRect)rect
    {
        NSArray* attributesToReturn = [self layoutAttributesForElementsInRect:rect];
        for (UICollectionViewLayoutAttributes* attributes in attributesToReturn)
        {
            if (nil == attributes.representedElementKind)
            {
                NSIndexPath* indexPath = attributes.indexPath;
                attributes.frame = [self layoutAttributesForItemAtIndexPath:indexPath].frame;
            }
        }
        return attributesToReturn;
    }
    
    - (UICollectionViewLayoutAttributes *)layoutAttributesForItemAtIndexPath:(NSIndexPath *)indexPath
    {
         UICollectionViewLayoutAttributes* currentItemAttributes = [self layoutAttributesForItemAtIndexPath:indexPath];
    
        if (indexPath.item < numColumns){
            CGRect f = currentItemAttributes.frame;
            f.origin.y = 0;
            currentItemAttributes.frame = f;
            return currentItemAttributes;
        }
        NSIndexPath* ipPrev = [NSIndexPath indexPathForItem:indexPath.item-numColumns inSection:indexPath.section];
        CGRect fPrev = [self layoutAttributesForItemAtIndexPath:ipPrev].frame;
        CGFloat YPointNew = fPrev.origin.y + fPrev.size.height + 10;
        CGRect f = currentItemAttributes.frame;
        f.origin.y = YPointNew;
        currentItemAttributes.frame = f;
        return currentItemAttributes;
    }