ios - 更新节标题中的文本 (UICollectionReusableView)

标签 ios uicollectionview sectionheader uicollectionreusableview

如何更新 UICollectionView 部分标题?我的 Collection View 中部分的标题(标题)显示每个部分可用的项目总数,当用户从集合中删除项目时,我需要更新该标题。

我正在实现数据源方法 collectionView:viewForSupplementaryElementOfKind:atIndexPath: 来为我的 Collection View 中的每个部分设置自定义 header ,如下所示:

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

    UICollectionReusableView *view = nil;

    if([kind isEqualToString:UICollectionElementKindSectionHeader]) {

        view = [collectionView dequeueReusableSupplementaryViewOfKind:kind withReuseIdentifier:@"myCustomCollectionHeader" forIndexPath:indexPath];

        MyCustomCollectionViewHeader *header = (MyCustomCollectionViewHeader *) view;

        NSString *headerTitle;

        if(indexPath.Section == 0) {
            headerTitle = [NSString stringWithFormat:@"%lu items", (unsigned long) myArrayOfObjectsInFirstSection.count];
        } else {
            headerTitle = [NSString stringWithFormat:@"%lu items", (unsigned long) myArrayOfObjectsInSecondSection.count];
        }

        header.myLabelTitle.text = headerTitle;
    }
    return view;
}

我的删除函数如下:

- (void)deleteSelectedItems {

    NSArray *indexPaths = self.collectionView.indexPathsForSelectedItems;

    for(NSIndexPath *indexPath in indexPaths) {

        NSString *numberOfItems;

        if(indexPath.section == 0) {
            [myArrayOfObjectsInFirstSection removeObjectAtIndex:indexPath.row];
            numberOfItems = [NSString stringWithFormat:@"%lu items", (unsigned long)myArrayOfObjectsInFirstSection.count];
        } else {
            [myArrayOfObjectsInSecondSection removeObjectAtIndex:indexPath.row];
            numberOfItems = [NSString stringWithFormat:@"%lu items", (unsigned long)myArrayOfObjectsInSecondSection.count];
        }

        [self.collectionView deleteItemsAtIndexPaths:@[indexPath]];
    }
    /* after deleting all items, section title must be updated with the new value of numberOfItems*/
}

我的应用程序能够在应用程序启动时设置 Collection View 中的项目数,但是从 Collection View 中删除项目后,标题不会更新。

请多多指教

最佳答案

经过多次尝试,我想出了以下解决方法:

为每个部分的补充元素设置一个标签值...然后可以通过标签搜索对象:

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

    UICollectionReusableView *view = nil;

    if([kind isEqualToString:UICollectionElementKindSectionHeader]) {

        view = [collectionView dequeueReusableSupplementaryViewOfKind:kind withReuseIdentifier:@"myCustomCollectionHeader" forIndexPath:indexPath];

        MyCustomCollectionViewHeader *header = (MyCustomCollectionViewHeader *) view;

        if(indexPath.Section == 0) {
            header.tag = 100;
        } else {
            header.tag = 200;
        }

        // ...

    }
    return view;
}

- (void)updateSectionTitles {
    NSInteger numberOfItems;
    MyCollectionViewHeader *header;
    if(indexPath.section == 0) {
        header = (MyCollectionViewHeader *) [self.collectionView viewWithTag:100];
        numberOfItems = myArrayOfObjectsInFirstSection.count;
    } else {
        header = (MyCollectionViewHeader *) [self.collectionView viewWithTag:200];
        numberOfItems = myArrayOfObjectsInSecondSection.count;
    }
    header.myHeaderTitleLabel.text = [NSString stringWithFormat:@"There are %lu items", (unsigned long) numberOfItems];
    }
}

在我个人看来,我认为不应该去更新 UICollectionView 中的任何部分补充元素。

另一种选择是调用重新加载部分,但这不仅会重新加载补充元素(页眉、页脚),还会重新加载每个部分的数据,这可能是一个性能问题:

[self.collectionView reloadSections:[NSIndexSet indexSetWithIndexesInRange:NSMakeRange(0, 2)]];

希望这可以帮助将来的任何人或以更好的方法发帖:)

关于ios - 更新节标题中的文本 (UICollectionReusableView),我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/24320152/

相关文章:

swift - 如何清除TableView并重新加载数据?

android - 自定义 ListView 部分标题

ios - 当您滚动时会改变的单个静态标题

ios - 纵向模式下的横向框架

ios - CAEmitterLayer 未显示

ios - 可以流式传输超过 115kbps 的视频吗?

swift - 集合单元格中的一行单元格快速吗?

ios - UIcollectionview支持覆盖流

ios - CollectionView 中的最后一项未对齐 - Swift

iphone - 使用 TTSectionedDataSource 将图像和标题文本添加到 Three20 表部分