@@ -259,30 +259,37 @@ - (void)collectionView:(UICollectionView *)collectionView didSelectItemAtIndexPa
259
259
}
260
260
261
261
- (UICollectionReusableView *)collectionView : (UICollectionView *)collectionView viewForSupplementaryElementOfKind : (NSString *)kind atIndexPath : (NSIndexPath *)indexPath {
262
- ZJCalendarCollectionReusableView *headerView = [collectionView dequeueReusableSupplementaryViewOfKind: UICollectionElementKindSectionHeader withReuseIdentifier: @" ZJCalendarCollectionReusableView" forIndexPath: indexPath];
263
- ZJCalendarMonth *month = self.datas [indexPath.section];
264
- headerView.titleLabel .text = [self .dateFormatter stringFromDate: month.date];
265
- return headerView;
262
+ if ([kind isEqualToString: UICollectionElementKindSectionHeader]) {
263
+ ZJCalendarCollectionReusableView *headerView = [collectionView dequeueReusableSupplementaryViewOfKind: UICollectionElementKindSectionHeader withReuseIdentifier: @" ZJCalendarCollectionReusableView" forIndexPath: indexPath];
264
+ ZJCalendarMonth *month = self.datas [indexPath.section];
265
+ headerView.titleLabel .text = [self .dateFormatter stringFromDate: month.date];
266
+ headerView.indexSection = indexPath.section ; // 标记当前的section
267
+ return headerView;
268
+ } else {
269
+ return nil ;
270
+ }
266
271
}
267
272
268
273
- (void )scrollViewDidScroll : (UIScrollView *)scrollView {
269
274
270
- NSArray <NSIndexPath *> *indexPaths = [self .collectionView indexPathsForVisibleSupplementaryElementsOfKind: UICollectionElementKindSectionHeader];
271
- for (NSIndexPath *indexP in indexPaths) {
272
- ZJCalendarCollectionReusableView *headerView = [self .collectionView dequeueReusableSupplementaryViewOfKind: UICollectionElementKindSectionHeader withReuseIdentifier: @" ZJCalendarCollectionReusableView" forIndexPath: indexP];
275
+ NSArray <UICollectionReusableView *> *indexPaths = [self .collectionView visibleSupplementaryViewsOfKind: UICollectionElementKindSectionHeader];
276
+ if (!indexPaths || indexPaths.count == 0 ) return ;
277
+
278
+ for (UICollectionReusableView *view in indexPaths) {
279
+ ZJCalendarCollectionReusableView *headerView = (ZJCalendarCollectionReusableView *)view;
273
280
274
281
CGRect rect = [headerView.superview convertRect: headerView.frame toView: self .contentView];
275
282
if (rect.origin .y <0 || rect.origin .y >500 ){
276
283
return ;
277
284
}
278
285
if (rect.origin .y <=70 ) {
279
- ZJCalendarMonth *month = self.datas [indexP.section ];
286
+ ZJCalendarMonth *month = self.datas [headerView.indexSection ];
280
287
self.weekTitleView .titleLabel .text = [self .dateFormatter stringFromDate: month.date];
281
288
return ;
282
289
}
283
290
if (rect.origin .y >70 ) {
284
- if (indexP. section - 1 >= 0 ){
285
- ZJCalendarMonth *month = self.datas [indexP.section -1 ];
291
+ if (headerView. indexSection - 1 >= 0 ){
292
+ ZJCalendarMonth *month = self.datas [headerView.indexSection -1 ];
286
293
self.weekTitleView .titleLabel .text = [self .dateFormatter stringFromDate: month.date];
287
294
return ;
288
295
}
0 commit comments