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*/
}
私のアプリは、アプリの起動時にコレクション ビューのアイテム数を設定できますが、コレクション ビューからアイテムを削除した後、ヘッダーが更新されません。
ご意見をお聞かせください