Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

UICollectionView: Show label "No item" in the section that don't have any item

I have a UICollectionView with 5 sections, some sections have data and some sections (in my code it is section 2) doesn't have (it depend on sever)
Therefore, I want to display a label ("No item") in the selection that doesn't data.

However, I can find any idea to do that, I hope anyone can give me some suggestion or instruction to achieve it.
I would really appreciate any help

Here is my code for intergrade sections

-(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;
 }


- (NSInteger)collectionView:(UICollectionView *)collectionView numberOfItemsInSection:(NSInteger)section{
    switch (section) {
        case 0:
            return 3;
        case 1:
            return 0; // it doesn't have any item
        case 2:
            return 2;
        case 3:
            return 3;
        case 4:
            return 5;
        default:
            return 0;
    }
}

- (FriendsCollectionViewCell *)collectionView:(UICollectionView *)collectionView cellForItemAtIndexPath:(NSIndexPath *)indexPath{
        FriendsCollectionViewCell *cell = [collectionView dequeueReusableCellWithReuseIdentifier:@"FriendsCollectionViewCell" forIndexPath:indexPath];

        [cell.lblFriendBand setText:@"Band: White Mash  "];
        [cell.lblFriendGenre setText:@"Freestyle house,  House,  Freestyle music,"];
        [cell.lblFriendECScore setText:@"EC score: 79"];

        return cell;
}

enter image description here

============================================

HERE IS WHAT I WANT

enter image description here

like image 648
Linh Avatar asked Dec 30 '15 09:12

Linh


1 Answers

Let's assume that you have your data (items) for each section in a NSArray.

So you have the goldSectionItems array, silverSectionItems array, bronzeSectionItems array, greenSectionItems array and otherSectionItems array.

What you want to do is:

  1. when you have some items in the section you want to display them
  2. when you have no items in the section you want to display "No item"

In case 1, you want to indicate to the collection view the number of items you have in your section, using your array that contains your items.

In case 2, you want to indicate to the collection view that you have 1 item, which will be the "No item" 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 its two operands. For example if your silverSectionItems array is empty then the count property will return 0, so the MAX(1, 0) will return 1. If your silverSectionItems is not empty count will return N (where N>1) so the MAX(1, N) will return N.

Then in your -collectionView:cellForItemAtIndexPath: you want to check in which case you are:

If you are in case 1 you want a cell that displays normal content.

If you are in case 2 you want a cell that displays "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 to ask anything you do not understand.

like image 186
averello Avatar answered Oct 27 '22 01:10

averello