gpt4 book ai didi

ios - 更新节标题中的文本 (UICollectionReusableView)

转载 作者:塔克拉玛干 更新时间:2023-11-01 21:21:57 26 4
gpt4 key购买 nike

如何更新 UICollectionView 部分标题?我的 Collection View 中部分的标题(标题)显示每个部分可用的项目总数,当用户从集合中删除项目时,我需要更新该标题。

我正在实现数据源方法 collectionView:viewForSupplementaryElementOfKind:atIndexPath: 来为我的 Collection View 中的每个部分设置自定义 header ,如下所示:

- (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*/
}

我的应用程序能够在应用程序启动时设置 Collection View 中的项目数,但是从 Collection View 中删除项目后,标题不会更新。

请多多指教

最佳答案

经过多次尝试,我想出了以下解决方法:

为每个部分的补充元素设置一个标签值...然后可以通过标签搜索对象:

- (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 中的任何部分补充元素。

另一种选择是调用重新加载部分,但这不仅会重新加载补充元素(页眉、页脚),还会重新加载每个部分的数据,这可能是一个性能问题:

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

希望这可以帮助将来的任何人或以更好的方法发帖:)

关于ios - 更新节标题中的文本 (UICollectionReusableView),我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/24320152/

26 4 0
Copyright 2021 - 2024 cfsdn All Rights Reserved 蜀ICP备2022000587号
广告合作:1813099741@qq.com 6ren.com