Suppose you have data (elements) for each section in an NSArray .
So you have an array of goldSectionItems , an array of silverSectionItems , an array of bronzeSectionItems , an array of greenSectionItems and an array of otherSectionItems .
What do you want to do:
- when you have some items in the section that you want to display.
- when you have no items in the section that you want to display "No item"
In case 1, you want to indicate in the form of a collection the number of elements that you have in your section using an array containing your elements.
In case 2, you want to indicate in the collection view that you have 1 element, which will be the "No Elements" cell.
- (NSInteger)collectionView:(UICollectionView *)collectionView numberOfItemsInSection:(NSInteger)section { switch (section) { case 0: return MAX(1, goldSectionItems.count); case 1: // return at least 1 when you have no items from the server. // When you do not have any items in // you NSArray then you return 1, otherwise you return // the number of items in your array return MAX(1, silverSectionItems.count); case 2: return MAX(1, bronzeSectionItems.count); case 3: return MAX(1, greenSectionItems.count); case 4: return MAX(1, otherSectionItems.count); default: return 0; } }
Note. MAX will return the maximum value between two operands. For example, if your silverSectionItems array silverSectionItems empty, the count property will return 0 , so MAX(1, 0) will return 1. If your silverSectionItems not empty, count will return N (where N>1 ), so MAX(1, N) will return N
Then in your -collectionView:cellForItemAtIndexPath: you want to check in which case you:
If you are in case 1, you want the cell to display normal content.
If you are in case 2, you want the cell to display “No item”.
- (FriendsCollectionViewCell *)collectionView:(UICollectionView *)collectionView cellForItemAtIndexPath:(NSIndexPath *)indexPath{ FriendsCollectionViewCell *cell = [collectionView dequeueReusableCellWithReuseIdentifier:@"FriendsCollectionViewCell" forIndexPath:indexPath]; // get the array that contains the items for your indexPath NSArray *items = [self itemArrayForIndexPath:indexPath]; // case 2 // if the section does not have any items then set up the // cell to display "No item" if (items.count == 0) { [cell.lblFriendBand setText:@"No item"]; [cell.lblFriendGenre setText:@""]; [cell.lblFriendECScore setText:@""]; } // case 1 // setup the cell with your items else { // get you item here and set up the cell with your content // Item *item = items[indexPath.item]; [cell.lblFriendBand setText:@"Band: White Mash "]; [cell.lblFriendGenre setText:@"Freestyle house, House, Freestyle music,"]; [cell.lblFriendECScore setText:@"EC score: 79"]; } return cell; } // return array for the corresponding indexPath - (NSArray *)itemArrayForIndexPath:(NSIndexPath *)indexPath { switch (indexPath.section) { case 0: return goldSectionItems; case 1: return silverSectionItems; case 2: return bronzeSectionItems; case 3: return greenSectionItems; case 4: return otherSectionItems; default: return nil; } } -(UICollectionReusableView *) collectionView:(UICollectionView *)collectionView viewForSupplementaryElementOfKind:(NSString *)kind atIndexPath:(NSIndexPath *)indexPath{ FriendsFanLevelHeaderView *headerView = (FriendsFanLevelHeaderView *)[self.collectionView dequeueReusableSupplementaryViewOfKind:UICollectionElementKindSectionHeader withReuseIdentifier:@"FanLevelHeader" forIndexPath:indexPath]; switch (indexPath.section) { case 0: [headerView.lblFanLevelTitle setText:@"Gold"]; break; case 1: [headerView.lblFanLevelTitle setText:@"Silver"]; break; case 2: [headerView.lblFanLevelTitle setText:@"Bronze"]; break; case 3: [headerView.lblFanLevelTitle setText:@"Green"]; break; case 4: [headerView.lblFanLevelTitle setText:@"Other"]; break; default: break; } return headerView; }
Feel that you don’t understand anything.