Skip to content

Commit df6de94

Browse files
committed
1、兼容iOS18,修复ZJCalendarView组件因UICollectionReusableView遍历异常导致的闪退问题;
1 parent 56c60ec commit df6de94

File tree

2 files changed

+19
-10
lines changed

2 files changed

+19
-10
lines changed

ZJBaseUtils/ZJCalendar/ZJCalendarCollectionReusableView.h

Lines changed: 2 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -13,6 +13,8 @@ NS_ASSUME_NONNULL_BEGIN
1313
@interface ZJCalendarCollectionReusableView : UICollectionReusableView
1414

1515
@property (nonatomic, strong) UILabel *titleLabel;
16+
/// 当前Section
17+
@property (nonatomic, assign) NSInteger indexSection;
1618

1719
@end
1820

ZJBaseUtils/ZJCalendar/ZJCalendarView.m

Lines changed: 17 additions & 10 deletions
Original file line numberDiff line numberDiff line change
@@ -259,30 +259,37 @@ - (void)collectionView:(UICollectionView *)collectionView didSelectItemAtIndexPa
259259
}
260260

261261
- (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+
}
266271
}
267272

268273
- (void)scrollViewDidScroll:(UIScrollView *)scrollView {
269274

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;
273280

274281
CGRect rect = [headerView.superview convertRect:headerView.frame toView:self.contentView];
275282
if (rect.origin.y<0 || rect.origin.y>500){
276283
return;
277284
}
278285
if (rect.origin.y<=70) {
279-
ZJCalendarMonth *month = self.datas[indexP.section];
286+
ZJCalendarMonth *month = self.datas[headerView.indexSection];
280287
self.weekTitleView.titleLabel.text = [self.dateFormatter stringFromDate:month.date];
281288
return;
282289
}
283290
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];
286293
self.weekTitleView.titleLabel.text = [self.dateFormatter stringFromDate:month.date];
287294
return;
288295
}

0 commit comments

Comments
 (0)