【问题标题】:Update text in section header (UICollectionReusableView)更新节标题中的文本 (UICollectionReusableView)
【发布时间】:2014-06-20 04:49:10
【问题描述】:

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

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

请指教

【问题讨论】:

    标签: ios uicollectionview sectionheader uicollectionreusableview


    【解决方案1】:

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

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

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

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

    【讨论】:

      【解决方案2】:

      如果你手头有NSIndexPath,当你想改变de header的时候,你可以通过调用来找回它

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

      然后使用tag 属性或使用

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

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

      干杯。

      【讨论】:

      • header 似乎总是为零.. 因为我在willDisplaySupplementaryView 中执行操作。我猜那一刻,视图还没有初始化?
      猜你喜欢
      • 2014-01-21
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      • 2015-01-01
      • 2013-03-01
      相关资源
      最近更新 更多