inVINCEable
inVINCEable

Reputation: 2206

UICollectionView reloads data but doesn't display new cells

I have a UICollection view that is populated by the array titlesMutable, when the view first loads the cells populate successfully (all 30 of them) when i add new data and do

[self.collectionView reloadData];

i can log titlesMutable in the number of items in section and the count is now 60. So it should be successful right? after the reload i can still use the collectionView how it normally is used after. Below is the code i have. Am i missing something or did i miss type something?

Set up CollectionView

    -(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;
    self.collectionView = [[UICollectionView alloc] initWithFrame:locOfScree collectionViewLayout:layout];
    self.collectionView.autoresizingMask = UIViewAutoresizingFlexibleHeight | UIViewAutoresizingFlexibleWidth;

    self.collectionView.delegate=self;
    self.collectionView.dataSource=self;

    [self.collectionView setDataSource:self];
    [self.collectionView setDelegate:self];

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

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

    self.collectionView = [[UICollectionView alloc] initWithFrame:self.view.bounds collectionViewLayout:layout];
    self.collectionView.autoresizingMask = UIViewAutoresizingFlexibleHeight | UIViewAutoresizingFlexibleWidth;
    self.collectionView.dataSource = self;
    self.collectionView.delegate = self;
    self.collectionView.backgroundColor = [UIColor whiteColor];
    [self.collectionView registerClass:[CHTCollectionViewWaterfallCell class]
        forCellWithReuseIdentifier:CellIdentifier];
}

CollectionView Methods

   - (NSInteger)collectionView:(UICollectionView *)collectionView numberOfItemsInSection:(NSInteger)section
{
    //return [self.imageURLStrings count];
    NSLog(@"Number of items in section:%lu", (unsigned long)[titlesMutable count]);
    return [titlesMutable count];
    //return 2;

}



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

    //    CHTCollectionViewWaterfallCell *cell =
    //    (CHTCollectionViewWaterfallCell *)[collectionView dequeueReusableCellWithReuseIdentifier:CellIdentifier
    //                                                                          forIndexPath:indexPath];
    CollectionViewCell *cell = (CollectionViewCell *)[collectionView dequeueReusableCellWithReuseIdentifier:CellIdentifier forIndexPath:indexPath];

    cell.layer.shouldRasterize = YES;
    cell.layer.rasterizationScale = [UIScreen mainScreen].scale;

    NSString *title = [titlesMutable objectAtIndex:indexPath.row];
    cell.backgroundColor = [UIColor whiteColor];
    NSString *imageUrl = [thumbMediaUrl objectForKey:title];
    UIImageView *imageView = [[UIImageView alloc] init];
    NSURLRequest *request = [NSURLRequest requestWithURL:[NSURL URLWithString:imageUrl]];
    [NSURLConnection sendAsynchronousRequest:request queue:[NSOperationQueue mainQueue] completionHandler:^(NSURLResponse *response, NSData *data, NSError *error) {
        //[cell.imageView setImage:[UIImage imageWithData:data]];
        [imageView setImage:[UIImage imageWithData:data]];
        cell.backgroundView = imageView;

    }];

    //cell.backgroundView = imageView;
    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 = [titlesMutable 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;
}

Add more data and reload

-(void)addMore {
 titlesMutable = [[NSMutableArray alloc] initWithArray:titles];

dispatch_async(dispatch_get_main_queue(), ^{
    [self.collectionView reloadData];



    NSLog(@"Im in the block");
    [self doneLoading];

});

}

Upvotes: 0

Views: 1554

Answers (1)

Zaid Daghestani
Zaid Daghestani

Reputation: 8615

You're creating two UICollectionViews, so when you call [self.collectionview reloadData] only the second collectionview is being called.

first collection

self.collectionView = [[UICollectionView alloc] initWithFrame:locOfScree collectionViewLayout:layout];
self.collectionView.autoresizingMask = UIViewAutoresizingFlexibleHeight | UIViewAutoresizingFlexibleWidth;

self.collectionView.delegate=self;
self.collectionView.dataSource=self;

[self.collectionView setDataSource:self];
[self.collectionView setDelegate:self];

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

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

second collection

self.collectionView = [[UICollectionView alloc] initWithFrame:self.view.bounds collectionViewLayout:layout];
self.collectionView.autoresizingMask = UIViewAutoresizingFlexibleHeight | UIViewAutoresizingFlexibleWidth;
self.collectionView.dataSource = self;
self.collectionView.delegate = self;
self.collectionView.backgroundColor = [UIColor whiteColor];
[self.collectionView registerClass:[CHTCollectionViewWaterfallCell class]
    forCellWithReuseIdentifier:CellIdentifier];

It looks like you want the first collection to be reloading data, not the second one.

Upvotes: 2

Related Questions