3
This commit is contained in:
@@ -21,7 +21,9 @@ NS_ASSUME_NONNULL_BEGIN
|
||||
|
||||
- (void)setItems:(NSArray<NSString *> *)items;
|
||||
|
||||
/// 在指定 index 上显示/隐藏加载指示(若 cell 不可见,内部会记录状态,待出现时应用)
|
||||
- (void)setLoading:(BOOL)loading atIndex:(NSInteger)index;
|
||||
|
||||
@end
|
||||
|
||||
NS_ASSUME_NONNULL_END
|
||||
|
||||
|
||||
@@ -10,6 +10,7 @@ static NSString * const kKBFunctionTagCellId2 = @"KBFunctionTagCellId2";
|
||||
@interface KBFunctionTagListView () <UICollectionViewDataSource, UICollectionViewDelegateFlowLayout>
|
||||
@property (nonatomic, strong) UICollectionView *collectionViewInternal;
|
||||
@property (nonatomic, copy) NSArray<NSString *> *items;
|
||||
@property (nonatomic, strong) NSMutableSet<NSNumber *> *loadingIndexes; // 记录需要展示loading的index
|
||||
@end
|
||||
|
||||
@implementation KBFunctionTagListView
|
||||
@@ -31,6 +32,7 @@ static NSString * const kKBFunctionTagCellId2 = @"KBFunctionTagCellId2";
|
||||
[_collectionViewInternal.trailingAnchor constraintEqualToAnchor:self.trailingAnchor],
|
||||
]];
|
||||
_items = @[];
|
||||
_loadingIndexes = [NSMutableSet set];
|
||||
}
|
||||
return self;
|
||||
}
|
||||
@@ -46,6 +48,8 @@ static NSString * const kKBFunctionTagCellId2 = @"KBFunctionTagCellId2";
|
||||
- (__kindof UICollectionViewCell *)collectionView:(UICollectionView *)collectionView cellForItemAtIndexPath:(NSIndexPath *)indexPath {
|
||||
KBFunctionTagCell *cell = [collectionView dequeueReusableCellWithReuseIdentifier:kKBFunctionTagCellId2 forIndexPath:indexPath];
|
||||
cell.titleLabel.text = (indexPath.item < self.items.count) ? self.items[indexPath.item] : @"";
|
||||
BOOL loading = [self.loadingIndexes containsObject:@(indexPath.item)];
|
||||
[cell setLoading:loading];
|
||||
return cell;
|
||||
}
|
||||
|
||||
@@ -65,4 +69,15 @@ static NSString * const kKBFunctionTagCellId2 = @"KBFunctionTagCellId2";
|
||||
}
|
||||
}
|
||||
|
||||
- (void)setLoading:(BOOL)loading atIndex:(NSInteger)index {
|
||||
NSNumber *key = @(index);
|
||||
if (loading) { [self.loadingIndexes addObject:key]; }
|
||||
else { [self.loadingIndexes removeObject:key]; }
|
||||
NSIndexPath *ip = [NSIndexPath indexPathForItem:index inSection:0];
|
||||
if (ip && ip.item < [self.collectionViewInternal numberOfItemsInSection:0]) {
|
||||
KBFunctionTagCell *cell = (KBFunctionTagCell *)[self.collectionViewInternal cellForItemAtIndexPath:ip];
|
||||
if ([cell isKindOfClass:[KBFunctionTagCell class]]) { [cell setLoading:loading]; }
|
||||
}
|
||||
}
|
||||
|
||||
@end
|
||||
|
||||
Reference in New Issue
Block a user