How can I update UICollectionView
section title? Headers (titles) for sections in my collection view show the total number of items available on each section and I need to update that title when the user has deleted items from the collection.
I'm implementing datasource method collectionView:viewForSupplementaryElementOfKind:atIndexPath:
to set a custom header for each one of the sections in my collection view, as follows:
- (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;
}
My delete function is as follows:
- (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*/
}
My app is able to set the number of items in collection view when the app starts, but after deleting items from collection view the header doesn't get updated.
Please advice