如何更新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*/
}
我的应用程序能够在应用程序启动时设置集合视图中的项目数,但在从集合视图中删除项目后,标题不会更新。
请指教