更新节头中的文本(UICollectionReusableView)

时间:2014-06-20 04:49:10

标签: ios uicollectionview sectionheader uicollectionreusableview

如何更新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*/
}

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

请咨询

2 个答案:

答案 0 :(得分: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)]];

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

答案 1 :(得分:2)

如果您想要更改de header时手中有NSIndexPath,可以通过调用

来检索它
let header = self.collectionView?.supplementaryViewForElementKind(UICollectionElementKindSectionHeader, atIndexPath: indexPath_)

然后使用tag属性或使用

检索标签
let _label = header!.subviews[0] as! UILabel

如果你有订单在标题中添加了子视图。

干杯。