3

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

我正在实现数据源方法collectionView:viewForSupplementaryElementOfKind:atIndexPath:来为我的集合视图中的每个部分设置一个自定义标题,如下所示:

- (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*/
}

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

请指教

4

2 回答 2

3

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

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

- (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)]];

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

于 2014-06-26T21:19:30.517 回答
2

如果您NSIndexPath想更改 de 标头时手头有一个,可以通过调用来检索它

let header = self.collectionView?.supplementaryViewForElementKind(UICollectionElementKindSectionHeader, atIndexPath: indexPath_)

然后使用tag属性或使用检索您的标签

let _label = header!.subviews[0] as! UILabel

如果您有在标题中添加子视图的顺序。

干杯。

于 2016-07-22T17:12:32.013 回答