Deniz Yazar
Deniz Yazar

Reputation: 379

How to check if collectionview is empty xcode

I have a collection view. I want to make this :

If there is no images added to the collection view, I want an imageview to be visible. But if there is 1 or more images added to the collection, I want the imageview to become invisible

Upvotes: 4

Views: 3685

Answers (3)

delabahan
delabahan

Reputation: 69

check if collectionViewArray is not nill

if (self.collectionViewArray != nil) {
  // your code here
}

Upvotes: 0

Eytan Schulman
Eytan Schulman

Reputation: 566

Try this, assuming collectionViewArray is the name of your array you are loading into the collection view. Change collectionViewArray to the name of your array. This is also assuming your UIImageView is called imageView. Replace imageView with whatever your UIImageView is called.

-(void)testCollectionView {
    BOOL visible;
    if (collectionViewArray.count == 0) { 
         imageView.hidden = NO;
    } else if (collectionViewArray.count >= 1) {
         imageView.hidden = YES;
    }
}

Upvotes: 0

wottle
wottle

Reputation: 13630

Try to call this method in viewDidLoad, as well as anywhere the collection view could have it's contents modified. If assumes you have IBOutlets connected to your UICollectionView and your UIImageView:

- (void) showAppropriateView
{
    int numberOfItemsInCollection = [collectionView numberOfItemsInSection:0];
    if( numberOfItemsInCollection > 0 )
    {
        collectionView.hidden = NO;
        imageView.hidden = YES;
    }
    else
    {
        collectionView.hidden = YES;
        imageView.hidden = NO;
    }
}

I'm not at a Mac right now, so I can't be certain this will work, but I don't see any reason is wouldn't.

Upvotes: 6

Related Questions