如何更新UICollectionView
节标题?在我的集合视图中,部分的标题(title)显示每个部分上可用的项目总数,当用户从集合中删除项目时,我需要更新标题。
我正在实现数据源方法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*/
}
我的应用程序可以在应用程序启动时设置集合视图中的项目数量,但从集合视图中删除项目后,标题不会更新。
请建议
经过多次尝试,我想出了以下解决方案:
为每个section补充元素设置一个标签值…然后可以通过标签搜索对象:
- (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
中的任何section补充元素的方式。
另一个选择是调用reload sections,但这不仅会重新加载补充元素(header, footers),还会重新加载每个section的数据,这可能会导致性能问题:
[self.collectionView reloadSections:[NSIndexSet indexSetWithIndexesInRange:NSMakeRange(0, 2)]];
希望这可以帮助任何人在未来或张贴更好的方法:)
如果你有一个NSIndexPath
在你的手中,当你想改变头,你可以通过调用
let header = self.collectionView?.supplementaryViewForElementKind(UICollectionElementKindSectionHeader, atIndexPath: indexPath_)
,然后使用tag
属性或使用
let _label = header!.subviews[0] as! UILabel
如果你有子视图在标题中添加的顺序。
欢呼。