Thanks to visit codestin.com
Credit goes to github.com

Skip to content

Commit 5b3f4f5

Browse files
committed
修复低版本数据异常闪退的问题
1 parent 4515193 commit 5b3f4f5

File tree

5 files changed

+160
-87
lines changed

5 files changed

+160
-87
lines changed

Classes/Private/CollectionView/IMYAOPCollectionViewUtils+DataSource.m

Lines changed: 36 additions & 11 deletions
Original file line numberDiff line numberDiff line change
@@ -9,30 +9,42 @@
99
#import "IMYAOPCollectionViewUtils+DataSource.h"
1010
#import "IMYAOPCollectionViewUtils+Private.h"
1111

12-
#define kAOPRealIndexPathCode \
12+
#define kAOPUserIndexPathCode \
1313
NSIndexPath *userIndexPath = [self userIndexPathByFeeds:indexPath]; \
1414
id<IMYAOPCollectionViewDataSource> dataSource = nil; \
1515
if (userIndexPath) { \
1616
dataSource = (id)self.origDataSource; \
1717
indexPath = userIndexPath; \
1818
} else { \
1919
dataSource = self.dataSource; \
20+
isInjectAction = YES; \
21+
} \
22+
if (isInjectAction) { \
23+
self.isUICalling += 1; \
2024
}
2125

22-
#define kAOPRealSectionCode \
26+
#define kAOPUserSectionCode \
2327
NSInteger userSection = [self userSectionByFeeds:section]; \
2428
id<IMYAOPCollectionViewDataSource> dataSource = nil; \
2529
if (userSection >= 0) { \
2630
dataSource = (id)self.origDataSource; \
2731
section = userSection; \
2832
} else { \
2933
dataSource = self.dataSource; \
34+
isInjectAction = YES; \
35+
} \
36+
if (isInjectAction) { \
37+
self.isUICalling += 1; \
3038
}
3139

32-
#define kAOPUICallingSaved \
40+
#define kAOPUICallingSaved \
41+
BOOL isInjectAction = NO; \
3342
self.isUICalling -= 1;
3443

35-
#define kAOPUICallingResotre \
44+
#define kAOPUICallingResotre \
45+
if (isInjectAction) { \
46+
self.isUICalling -= 1; \
47+
} \
3648
self.isUICalling += 1;
3749

3850
@implementation IMYAOPCollectionViewUtils (UICollectionViewDataSource)
@@ -80,7 +92,7 @@ - (NSInteger)collectionView:(UICollectionView *)collectionView numberOfItemsInSe
8092

8193
- (UICollectionViewCell *)collectionView:(UICollectionView *)collectionView cellForItemAtIndexPath:(NSIndexPath *)indexPath {
8294
kAOPUICallingSaved;
83-
kAOPRealIndexPathCode;
95+
kAOPUserIndexPathCode;
8496
UICollectionViewCell *cell = nil;
8597
if ([dataSource respondsToSelector:@selector(collectionView:cellForItemAtIndexPath:)]) {
8698
cell = [dataSource collectionView:collectionView cellForItemAtIndexPath:indexPath];
@@ -97,7 +109,7 @@ - (UICollectionViewCell *)collectionView:(UICollectionView *)collectionView cell
97109

98110
- (UICollectionReusableView *)collectionView:(UICollectionView *)collectionView viewForSupplementaryElementOfKind:(NSString *)kind atIndexPath:(NSIndexPath *)indexPath {
99111
kAOPUICallingSaved;
100-
kAOPRealIndexPathCode;
112+
kAOPUserIndexPathCode;
101113
UICollectionReusableView *reusableView = nil;
102114
if ([dataSource respondsToSelector:@selector(collectionView:viewForSupplementaryElementOfKind:atIndexPath:)]) {
103115
reusableView = [dataSource collectionView:collectionView viewForSupplementaryElementOfKind:kind atIndexPath:indexPath];
@@ -114,7 +126,7 @@ - (UICollectionReusableView *)collectionView:(UICollectionView *)collectionView
114126

115127
- (BOOL)collectionView:(UICollectionView *)collectionView canMoveItemAtIndexPath:(NSIndexPath *)indexPath {
116128
kAOPUICallingSaved;
117-
kAOPRealIndexPathCode;
129+
kAOPUserIndexPathCode;
118130
BOOL canMove = NO;
119131
if ([dataSource respondsToSelector:@selector(collectionView:canMoveItemAtIndexPath:)]) {
120132
canMove = [dataSource collectionView:collectionView canMoveItemAtIndexPath:indexPath];
@@ -124,6 +136,7 @@ - (BOOL)collectionView:(UICollectionView *)collectionView canMoveItemAtIndexPath
124136
}
125137

126138
- (void)collectionView:(UICollectionView *)collectionView moveItemAtIndexPath:(NSIndexPath *)sourceIndexPath toIndexPath:(NSIndexPath *)destinationIndexPath {
139+
// 只给业务方回调
127140
kAOPUICallingSaved;
128141
NSIndexPath *source = [self userIndexPathByFeeds:sourceIndexPath];
129142
NSIndexPath *destin = [self userIndexPathByFeeds:destinationIndexPath];
@@ -134,13 +147,25 @@ - (void)collectionView:(UICollectionView *)collectionView moveItemAtIndexPath:(N
134147
}
135148

136149
- (NSArray<NSString *> *)indexTitlesForCollectionView:(UICollectionView *)collectionView {
137-
NSAssert(NO, @"NO Impl");
138-
return nil;
150+
// 只给业务方回调
151+
kAOPUICallingSaved;
152+
NSArray<NSString *> *titles = @[];
153+
if ([self.origDataSource respondsToSelector:@selector(indexTitlesForCollectionView:)]) {
154+
titles = [self.origDataSource indexTitlesForCollectionView:collectionView];
155+
}
156+
kAOPUICallingResotre;
157+
return titles;
139158
}
140159

141160
- (NSIndexPath *)collectionView:(UICollectionView *)collectionView indexPathForIndexTitle:(NSString *)title atIndex:(NSInteger)index {
142-
NSAssert(NO, @"NO Impl");
143-
return nil;
161+
// 只给业务方回调
162+
kAOPUICallingSaved;
163+
NSIndexPath *indexPath = nil;
164+
if ([self.origDataSource respondsToSelector:@selector(collectionView:indexPathForIndexTitle:atIndex:)]) {
165+
indexPath = [self.origDataSource collectionView:collectionView indexPathForIndexTitle:title atIndex:index];
166+
}
167+
kAOPUICallingResotre;
168+
return indexPath;
144169
}
145170

146171
@end

Classes/Private/CollectionView/IMYAOPCollectionViewUtils+Delegate.m

Lines changed: 35 additions & 23 deletions
Original file line numberDiff line numberDiff line change
@@ -9,39 +9,51 @@
99
#import "IMYAOPCollectionViewUtils+Delegate.h"
1010
#import "IMYAOPCollectionViewUtils+Private.h"
1111

12-
#define kAOPRealIndexPathCode \
12+
#define kAOPUserIndexPathCode \
1313
NSIndexPath *userIndexPath = [self userIndexPathByFeeds:indexPath]; \
1414
id<IMYAOPCollectionViewDelegate> delegate = nil; \
1515
if (userIndexPath) { \
1616
delegate = (id)self.origDelegate; \
1717
indexPath = userIndexPath; \
1818
} else { \
1919
delegate = self.delegate; \
20+
isInjectAction = YES; \
21+
} \
22+
if (isInjectAction) { \
23+
self.isUICalling += 1; \
2024
}
2125

22-
#define kAOPRealSectionCode \
26+
27+
#define kAOPUserSectionCode \
2328
NSInteger userSection = [self userSectionByFeeds:section]; \
2429
id<IMYAOPCollectionViewDelegate> delegate = nil; \
2530
if (userSection >= 0) { \
2631
delegate = (id)self.origDelegate; \
2732
section = userSection; \
2833
} else { \
2934
delegate = self.delegate; \
35+
isInjectAction = YES; \
36+
} \
37+
if (isInjectAction) { \
38+
self.isUICalling += 1; \
3039
}
3140

32-
#define kAOPUICallingSaved \
41+
42+
#define kAOPUICallingSaved \
43+
BOOL isInjectAction = NO; \
3344
self.isUICalling -= 1;
3445

35-
#define kAOPUICallingResotre \
46+
#define kAOPUICallingResotre \
47+
if (isInjectAction) { \
48+
self.isUICalling -= 1; \
49+
} \
3650
self.isUICalling += 1;
3751

38-
#define kAOPDefineLayout
39-
4052
@implementation IMYAOPCollectionViewUtils (UITableViewDelegate)
4153

4254
- (BOOL)collectionView:(UICollectionView *)collectionView shouldHighlightItemAtIndexPath:(NSIndexPath *)indexPath {
4355
kAOPUICallingSaved;
44-
kAOPRealIndexPathCode;
56+
kAOPUserIndexPathCode;
4557
BOOL canHighlight = YES;
4658
if ([delegate respondsToSelector:@selector(collectionView:shouldHighlightItemAtIndexPath:)]) {
4759
canHighlight = [delegate collectionView:collectionView shouldHighlightItemAtIndexPath:indexPath];
@@ -52,7 +64,7 @@ - (BOOL)collectionView:(UICollectionView *)collectionView shouldHighlightItemAtI
5264

5365
- (void)collectionView:(UICollectionView *)collectionView didHighlightItemAtIndexPath:(NSIndexPath *)indexPath {
5466
kAOPUICallingSaved;
55-
kAOPRealIndexPathCode;
67+
kAOPUserIndexPathCode;
5668
if ([delegate respondsToSelector:@selector(collectionView:didHighlightItemAtIndexPath:)]) {
5769
[delegate collectionView:collectionView didHighlightItemAtIndexPath:indexPath];
5870
}
@@ -61,7 +73,7 @@ - (void)collectionView:(UICollectionView *)collectionView didHighlightItemAtInde
6173

6274
- (void)collectionView:(UICollectionView *)collectionView didUnhighlightItemAtIndexPath:(NSIndexPath *)indexPath {
6375
kAOPUICallingSaved;
64-
kAOPRealIndexPathCode;
76+
kAOPUserIndexPathCode;
6577
if ([delegate respondsToSelector:@selector(collectionView:didUnhighlightItemAtIndexPath:)]) {
6678
[delegate collectionView:collectionView didUnhighlightItemAtIndexPath:indexPath];
6779
}
@@ -70,7 +82,7 @@ - (void)collectionView:(UICollectionView *)collectionView didUnhighlightItemAtIn
7082

7183
- (BOOL)collectionView:(UICollectionView *)collectionView shouldSelectItemAtIndexPath:(NSIndexPath *)indexPath {
7284
kAOPUICallingSaved;
73-
kAOPRealIndexPathCode;
85+
kAOPUserIndexPathCode;
7486
BOOL canSelected = YES;
7587
if ([delegate respondsToSelector:@selector(collectionView:shouldSelectItemAtIndexPath:)]) {
7688
canSelected = [delegate collectionView:collectionView shouldSelectItemAtIndexPath:indexPath];
@@ -81,7 +93,7 @@ - (BOOL)collectionView:(UICollectionView *)collectionView shouldSelectItemAtInde
8193

8294
- (BOOL)collectionView:(UICollectionView *)collectionView shouldDeselectItemAtIndexPath:(NSIndexPath *)indexPath {
8395
kAOPUICallingSaved;
84-
kAOPRealIndexPathCode;
96+
kAOPUserIndexPathCode;
8597
BOOL canSelected = YES;
8698
if ([delegate respondsToSelector:@selector(collectionView:shouldDeselectItemAtIndexPath:)]) {
8799
canSelected = [delegate collectionView:collectionView shouldDeselectItemAtIndexPath:indexPath];
@@ -92,7 +104,7 @@ - (BOOL)collectionView:(UICollectionView *)collectionView shouldDeselectItemAtIn
92104

93105
- (void)collectionView:(UICollectionView *)collectionView didSelectItemAtIndexPath:(NSIndexPath *)indexPath {
94106
kAOPUICallingSaved;
95-
kAOPRealIndexPathCode;
107+
kAOPUserIndexPathCode;
96108
if ([delegate respondsToSelector:@selector(collectionView:didSelectItemAtIndexPath:)]) {
97109
[delegate collectionView:collectionView didSelectItemAtIndexPath:indexPath];
98110
}
@@ -101,7 +113,7 @@ - (void)collectionView:(UICollectionView *)collectionView didSelectItemAtIndexPa
101113

102114
- (void)collectionView:(UICollectionView *)collectionView didDeselectItemAtIndexPath:(NSIndexPath *)indexPath {
103115
kAOPUICallingSaved;
104-
kAOPRealIndexPathCode;
116+
kAOPUserIndexPathCode;
105117
if ([delegate respondsToSelector:@selector(collectionView:didDeselectItemAtIndexPath:)]) {
106118
[delegate collectionView:collectionView didDeselectItemAtIndexPath:indexPath];
107119
}
@@ -114,7 +126,7 @@ - (void)collectionView:(UICollectionView *)collectionView willDisplayCell:(UICol
114126
if ([self.delegate respondsToSelector:@selector(aopCollectionUtils:willDisplayCell:forItemAtIndexPath:)]) {
115127
[self.delegate aopCollectionUtils:self willDisplayCell:cell forItemAtIndexPath:indexPath];
116128
}
117-
kAOPRealIndexPathCode;
129+
kAOPUserIndexPathCode;
118130
if ([delegate respondsToSelector:@selector(collectionView:willDisplayCell:forItemAtIndexPath:)]) {
119131
[delegate collectionView:collectionView willDisplayCell:cell forItemAtIndexPath:indexPath];
120132
}
@@ -123,7 +135,7 @@ - (void)collectionView:(UICollectionView *)collectionView willDisplayCell:(UICol
123135

124136
- (void)collectionView:(UICollectionView *)collectionView willDisplaySupplementaryView:(UICollectionReusableView *)view forElementKind:(NSString *)elementKind atIndexPath:(NSIndexPath *)indexPath {
125137
kAOPUICallingSaved;
126-
kAOPRealIndexPathCode;
138+
kAOPUserIndexPathCode;
127139
if ([delegate respondsToSelector:@selector(collectionView:willDisplaySupplementaryView:forElementKind:atIndexPath:)]) {
128140
[delegate collectionView:collectionView willDisplaySupplementaryView:view forElementKind:elementKind atIndexPath:indexPath];
129141
}
@@ -136,7 +148,7 @@ - (void)collectionView:(UICollectionView *)collectionView didEndDisplayingCell:(
136148
if ([self.delegate respondsToSelector:@selector(aopCollectionUtils:didEndDisplayingCell:forItemAtIndexPath:)]) {
137149
[self.delegate aopCollectionUtils:self didEndDisplayingCell:cell forItemAtIndexPath:indexPath];
138150
}
139-
kAOPRealIndexPathCode;
151+
kAOPUserIndexPathCode;
140152
if ([delegate respondsToSelector:@selector(collectionView:didEndDisplayingCell:forItemAtIndexPath:)]) {
141153
[delegate collectionView:collectionView didEndDisplayingCell:cell forItemAtIndexPath:indexPath];
142154
}
@@ -145,7 +157,7 @@ - (void)collectionView:(UICollectionView *)collectionView didEndDisplayingCell:(
145157

146158
- (void)collectionView:(UICollectionView *)collectionView didEndDisplayingSupplementaryView:(UICollectionReusableView *)view forElementOfKind:(NSString *)elementKind atIndexPath:(NSIndexPath *)indexPath {
147159
kAOPUICallingSaved;
148-
kAOPRealIndexPathCode;
160+
kAOPUserIndexPathCode;
149161
if ([delegate respondsToSelector:@selector(collectionView:didEndDisplayingSupplementaryView:forElementOfKind:atIndexPath:)]) {
150162
[delegate collectionView:collectionView didEndDisplayingSupplementaryView:view forElementOfKind:elementKind atIndexPath:indexPath];
151163
}
@@ -154,7 +166,7 @@ - (void)collectionView:(UICollectionView *)collectionView didEndDisplayingSupple
154166

155167
- (BOOL)collectionView:(UICollectionView *)collectionView shouldShowMenuForItemAtIndexPath:(NSIndexPath *)indexPath {
156168
kAOPUICallingSaved;
157-
kAOPRealIndexPathCode;
169+
kAOPUserIndexPathCode;
158170
BOOL canShowMenu = YES;
159171
if ([delegate respondsToSelector:@selector(collectionView:shouldShowMenuForItemAtIndexPath:)]) {
160172
canShowMenu = [delegate collectionView:collectionView shouldShowMenuForItemAtIndexPath:indexPath];
@@ -165,7 +177,7 @@ - (BOOL)collectionView:(UICollectionView *)collectionView shouldShowMenuForItemA
165177

166178
- (BOOL)collectionView:(UICollectionView *)collectionView canPerformAction:(SEL)action forItemAtIndexPath:(NSIndexPath *)indexPath withSender:(nullable id)sender {
167179
kAOPUICallingSaved;
168-
kAOPRealIndexPathCode;
180+
kAOPUserIndexPathCode;
169181
BOOL canPerform = YES;
170182
if ([delegate respondsToSelector:@selector(collectionView:canPerformAction:forItemAtIndexPath:withSender:)]) {
171183
canPerform = [delegate collectionView:collectionView canPerformAction:action forItemAtIndexPath:indexPath withSender:sender];
@@ -176,7 +188,7 @@ - (BOOL)collectionView:(UICollectionView *)collectionView canPerformAction:(SEL)
176188

177189
- (void)collectionView:(UICollectionView *)collectionView performAction:(SEL)action forItemAtIndexPath:(NSIndexPath *)indexPath withSender:(nullable id)sender {
178190
kAOPUICallingSaved;
179-
kAOPRealIndexPathCode;
191+
kAOPUserIndexPathCode;
180192
if ([delegate respondsToSelector:@selector(collectionView:performAction:forItemAtIndexPath:withSender:)]) {
181193
[delegate collectionView:collectionView performAction:action forItemAtIndexPath:indexPath withSender:sender];
182194
}
@@ -185,7 +197,7 @@ - (void)collectionView:(UICollectionView *)collectionView performAction:(SEL)act
185197

186198
- (BOOL)collectionView:(UICollectionView *)collectionView canFocusItemAtIndexPath:(NSIndexPath *)indexPath {
187199
kAOPUICallingSaved;
188-
kAOPRealIndexPathCode;
200+
kAOPUserIndexPathCode;
189201
BOOL canFocus = YES;
190202
if ([delegate respondsToSelector:@selector(collectionView:canFocusItemAtIndexPath:)]) {
191203
canFocus = [delegate collectionView:collectionView canFocusItemAtIndexPath:indexPath];
@@ -208,7 +220,7 @@ - (NSIndexPath *)collectionView:(UICollectionView *)collectionView targetIndexPa
208220

209221
- (BOOL)collectionView:(UICollectionView *)collectionView shouldSpringLoadItemAtIndexPath:(NSIndexPath *)indexPath withContext:(id<UISpringLoadedInteractionContext>)context {
210222
kAOPUICallingSaved;
211-
kAOPRealIndexPathCode;
223+
kAOPUserIndexPathCode;
212224
BOOL shouldSpringLoad = YES;
213225
if ([delegate respondsToSelector:@selector(collectionView:shouldSpringLoadItemAtIndexPath:withContext:)]) {
214226
shouldSpringLoad = [delegate collectionView:collectionView shouldSpringLoadItemAtIndexPath:indexPath withContext:context];
@@ -222,7 +234,7 @@ - (BOOL)collectionView:(UICollectionView *)collectionView shouldSpringLoadItemAt
222234
// water layout and flow layout
223235
- (CGSize)collectionView:(UICollectionView *)collectionView layout:(UICollectionViewLayout *)collectionViewLayout sizeForItemAtIndexPath:(NSIndexPath *)indexPath {
224236
kAOPUICallingSaved;
225-
kAOPRealIndexPathCode;
237+
kAOPUserIndexPathCode;
226238
CGSize size = CGSizeZero;
227239
if ([collectionViewLayout isKindOfClass:[UICollectionViewFlowLayout class]]) {
228240
size = ((UICollectionViewFlowLayout *)collectionViewLayout).itemSize;

0 commit comments

Comments
 (0)