UICollectionView:在没有任何项目的部分中显示标签"No item"

Pha*_*inh 5 objective-c collectionview ios

我有UICollectionView5个部分,一些部分有数据和一些部分(在我的代码中是第2部分)没有(它依赖于服务器)
因此,我想在选择中显示一个标签("无项目")那不是数据.

但是,我可以找到任何想法,我希望任何人都可以给我一些建议或指示来实现它.
我真的很感激任何帮助

这是我的intergrade部分代码

-(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;
}
Run Code Online (Sandbox Code Playgroud)

在此输入图像描述

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

这是我想要的

在此输入图像描述

Jak*_*kub 2

您需要使用UICollectionElementKindSectionFooter与使用节标题相同的方式。我构建了简单的 CollectionView 示例来展示我的想法:

在此输入图像描述

所以基本上我所做的是创建通用页脚并在有内容填充单元格时隐藏它:

基本示例是:

- (CGSize)collectionView:(UICollectionView *)collectionView layout:(UICollectionViewLayout*)collectionViewLayout referenceSizeForFooterInSection:(NSInteger)section {
    NSArray *sectionContent = self.collectionViewData[section][COLLECTIONVIEW_CONTENT_KEY];
    return ([sectionContent count] > 0) ? CGSizeMake(0, 0) : CGSizeMake(collectionView.frame.size.width, FOOTER_HEIGHT);
  }
Run Code Online (Sandbox Code Playgroud)

其中myself.collectionViewData是一个NSArrayNSDictionary .

但为了避免打破限制,我强烈建议为xib页脚创建单独的内容并将两者应用于您的 collectionView。

-(UICollectionReusableView *) collectionView:(UICollectionView *)collectionView viewForSupplementaryElementOfKind:(NSString *)kind atIndexPath:(NSIndexPath *)indexPath{

NSArray *sectionContent = self.collectionViewData[indexPath.section][COLLECTIONVIEW_CONTENT_KEY];

if([kind isEqualToString:UICollectionElementKindSectionHeader]) {
    UICollectionReusableView *headerView = [collectionView dequeueReusableSupplementaryViewOfKind:UICollectionElementKindSectionHeader withReuseIdentifier:@"header" forIndexPath:indexPath];
    return headerView;
} else {
    NSString *footerIdentifier = ([sectionContent count] > 0) ? @"blankFooter" : @"footer";
    UICollectionReusableView *footerView = [collectionView dequeueReusableSupplementaryViewOfKind:UICollectionElementKindSectionFooter withReuseIdentifier:footerIdentifier forIndexPath:indexPath];
    return footerView;
}
}
Run Code Online (Sandbox Code Playgroud)

您可以在这里下载示例项目来看看:https://bitbucket.org/Kettu/so_34526276

Bitbucket 到此存储库