我有UICollectionView
使用CSStickyHeaderFlowLayout
来模仿UITableView
中的标题行为。标题内有SegmentedControl
来控制UICollectionView
上的数据。所以我想要的是在我点击片段(调用API)并执行reloadData
时重新加载数据,但它总是调用
- (UICollectionReusableView *)collectionView:(UICollectionView *)collectionView viewForSupplementaryElementOfKind:(NSString *)kind atIndexPath:(NSIndexPath *)indexPath
那么只重新加载数据而不是标题的最佳方法是什么,因为当reloadData
标题重新加载时,该段将返回到第一个状态。
这是我viewForSupplementaryElementOfKind
- (UICollectionReusableView *)collectionView:(UICollectionView *)collectionView viewForSupplementaryElementOfKind:(NSString *)kind atIndexPath:(NSIndexPath *)indexPath
{
UICollectionReusableView *reusableView = nil;
if (kind == UICollectionElementKindSectionHeader) {
SegmentHeaderView *collectionHeader= [collectionView dequeueReusableSupplementaryViewOfKind:kind withReuseIdentifier:@"Header" forIndexPath:indexPath];
HMSegmentedControl *segmentedControl = [[HMSegmentedControl alloc] initWithSectionTitles:@[@"Popular", @"Lelang"]];
[segmentedControl setFrame:CGRectMake(0, 0, self.view.frame.size.width, 45)];
[segmentedControl addTarget:self action:@selector(segmentedControlChangedValue:) forControlEvents:UIControlEventValueChanged];
segmentedControl.backgroundColor = [NConfig FlatButtonGray];
segmentedControl.selectionIndicatorColor = [UIColor whiteColor];
segmentedControl.selectionIndicatorBoxOpacity=1;
segmentedControl.titleTextAttributes = @{NSForegroundColorAttributeName : [NConfig FlatButtonOrange]};
segmentedControl.selectedTitleTextAttributes = @{NSForegroundColorAttributeName : [NConfig FlatButtonOrange]};
segmentedControl.selectionStyle = HMSegmentedControlSelectionStyleBox;
segmentedControl.selectedSegmentIndex = HMSegmentedControlNoSegment;
segmentedControl.selectionIndicatorLocation = HMSegmentedControlSelectionIndicatorLocationNone;
segmentedControl.shouldAnimateUserSelection = NO;
[segmentedControl setSelectedSegmentIndex:0 animated:YES];
[collectionHeader addSubview:segmentedControl];
reusableView = collectionHeader;
}
return reusableView;
}
任何建议? :)
答案 0 :(得分:0)
您的错误是您只将选择内容存储在可能丢失的视图层内。当用户选择不同的段时,您确实应该使用新的选择索引设置属性。然后,您可以通过重新加载数据来对此更改的属性做出反应。在返回标题视图之前,将选定的段设置为先前存储的索引。这样选择永远不会丢失。
除此之外,您还应该避免使用reloadData,而只是重新加载实际更改的项目。