This commit is contained in:
2025-11-08 20:04:50 +08:00
parent faeb930fe3
commit 3b0beb52da
8 changed files with 620 additions and 17 deletions

View File

@@ -0,0 +1,169 @@
//
// KBSearchResultVC.m
// keyBoard
//
// KBSearchBarView UICollectionView
// - Masonry
// -
// -
//
#import "KBSearchResultVC.h"
#import "KBSearchBarView.h"
#import "KBSkinCardCell.h"
static NSString * const kResultCellId = @"KBSkinCardCell";
@interface KBSearchResultVC ()<UICollectionViewDataSource, UICollectionViewDelegateFlowLayout>
// KBSearchBarView
@property (nonatomic, strong) KBSearchBarView *searchBarView;
//
@property (nonatomic, strong) UICollectionView *collectionView;
@property (nonatomic, strong) UICollectionViewFlowLayout *flowLayout;
//
@property (nonatomic, strong) NSMutableArray<NSDictionary *> *resultItems; // @{title, price}
@end
@implementation KBSearchResultVC
- (void)viewDidLoad {
[super viewDidLoad];
self.view.backgroundColor = [UIColor whiteColor];
//
[self.view addSubview:self.searchBarView];
[self.view addSubview:self.collectionView];
// Masonry
[self.searchBarView mas_makeConstraints:^(MASConstraintMaker *make) {
// 16 40
make.top.equalTo(self.view.mas_top).offset(KB_NAV_TOTAL_HEIGHT + 8);
make.left.equalTo(self.view).offset(16);
make.right.equalTo(self.view).offset(-16);
make.height.mas_equalTo(40);
}];
[self.collectionView mas_makeConstraints:^(MASConstraintMaker *make) {
make.top.equalTo(self.searchBarView.mas_bottom).offset(12);
make.left.right.bottom.equalTo(self.view);
}];
//
if (self.defaultKeyword.length > 0) {
[self.searchBarView updateKeyword:self.defaultKeyword];
[self performSearch:self.defaultKeyword];
} else {
//
[self loadMockData];
}
}
#pragma mark - Private
///
- (void)performSearch:(NSString *)keyword {
// 10
[self.resultItems removeAllObjects];
for (int i = 0; i < 10; i++) {
[self.resultItems addObject:@{ @"title": @"Dopamine", @"price": @"20" }];
}
[self.collectionView reloadData];
}
///
- (void)loadMockData {
[self.resultItems removeAllObjects];
for (int i = 0; i < 12; i++) {
[self.resultItems addObject:@{ @"title": @"Dopamine", @"price": @"20" }];
}
[self.collectionView reloadData];
}
#pragma mark - UICollectionViewDataSource
- (NSInteger)numberOfSectionsInCollectionView:(UICollectionView *)collectionView {
return 1;
}
- (NSInteger)collectionView:(UICollectionView *)collectionView numberOfItemsInSection:(NSInteger)section {
return self.resultItems.count;
}
- (__kindof UICollectionViewCell *)collectionView:(UICollectionView *)collectionView cellForItemAtIndexPath:(NSIndexPath *)indexPath {
KBSkinCardCell *cell = [collectionView dequeueReusableCellWithReuseIdentifier:kResultCellId forIndexPath:indexPath];
NSDictionary *it = self.resultItems[indexPath.item];
[cell configWithTitle:it[@"title"] imageURL:nil price:it[@"price"]];
return cell;
}
#pragma mark - UICollectionViewDelegateFlowLayout
- (CGSize)collectionView:(UICollectionView *)collectionView layout:(UICollectionViewLayout*)collectionViewLayout sizeForItemAtIndexPath:(NSIndexPath *)indexPath {
//
CGFloat width = collectionView.bounds.size.width;
CGFloat inset = 16; //
CGFloat spacing = 12; //
CGFloat w = floor((width - inset * 2 - spacing) / 2.0);
CGFloat h = w * 0.75 + 8 + 20 + 10 + 6 + 8; // KBSkinCardCell
return CGSizeMake(w, h);
}
- (UIEdgeInsets)collectionView:(UICollectionView *)collectionView layout:(UICollectionViewLayout*)collectionViewLayout insetForSectionAtIndex:(NSInteger)section {
return UIEdgeInsetsMake(8, 16, 20, 16);
}
- (CGFloat)collectionView:(UICollectionView *)collectionView layout:(UICollectionViewLayout*)collectionViewLayout minimumInteritemSpacingForSectionAtIndex:(NSInteger)section {
return 12;
}
- (CGFloat)collectionView:(UICollectionView *)collectionView layout:(UICollectionViewLayout*)collectionViewLayout minimumLineSpacingForSectionAtIndex:(NSInteger)section {
return 16;
}
#pragma mark - Lazy
- (KBSearchBarView *)searchBarView {
if (!_searchBarView) {
_searchBarView = [[KBSearchBarView alloc] init];
_searchBarView.placeholder = @"Themes";
__weak typeof(self) weakSelf = self;
_searchBarView.onSearch = ^(NSString * _Nonnull keyword) {
[weakSelf performSearch:keyword];
};
}
return _searchBarView;
}
- (UICollectionViewFlowLayout *)flowLayout {
if (!_flowLayout) {
_flowLayout = [[UICollectionViewFlowLayout alloc] init];
_flowLayout.scrollDirection = UICollectionViewScrollDirectionVertical;
}
return _flowLayout;
}
- (UICollectionView *)collectionView {
if (!_collectionView) {
_collectionView = [[UICollectionView alloc] initWithFrame:CGRectZero collectionViewLayout:self.flowLayout];
_collectionView.backgroundColor = [UIColor whiteColor];
_collectionView.dataSource = self;
_collectionView.delegate = self;
// cell
[_collectionView registerClass:KBSkinCardCell.class forCellWithReuseIdentifier:kResultCellId];
}
return _collectionView;
}
- (NSMutableArray<NSDictionary *> *)resultItems {
if (!_resultItems) {
_resultItems = [NSMutableArray array];
}
return _resultItems;
}
@end