This commit is contained in:
2025-11-12 16:49:19 +08:00
parent fea22aecab
commit 2f4205ad1a
7 changed files with 144 additions and 18 deletions

View File

@@ -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

View File

@@ -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; // loadingindex
@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