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

もう 1 つのオプションは、セクションの再読み込みを呼び出すことですが、これは補足要素 (ヘッダー、フッター) だけでなく、パフォーマンスの問題となる可能性のある各セクションのデータも再読み込みします。

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

これが将来誰かを助けるか、より良いアプローチで投稿することを願っています:)

于 2014-06-26T21:19:30.517 に答える
2

NSIndexPathヘッダーを変更したいときに手元にある場合は、呼び出して取得できます

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

tag次に、プロパティまたはを使用してラベルを取得します

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

サブビューがヘッダーに追加された順序がある場合。

乾杯。

于 2016-07-22T17:12:32.013 に答える