1

我有一个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;
}

有什么建议吗?:)

4

1 回答 1

0

您的错误是您仅将选择存储在可能丢失的视图层中。当用户选择不同的段时,您确实应该使用新的选择索引设置一个属性。然后,您可以通过重新加载数据来响应此更改的属性。在返回标题视图之前,您将选定的段设置为您之前存储的索引。这样选择就不会丢失。

除此之外,您还应该避免使用 reloadData 而只重新加载实际更改的项目。

于 2015-05-12T06:40:13.650 回答