This commit is contained in:
2025-11-09 18:07:47 +08:00
parent dc9ee10023
commit 883b222254
2 changed files with 85 additions and 75 deletions

View File

@@ -11,11 +11,16 @@
NS_ASSUME_NONNULL_BEGIN NS_ASSUME_NONNULL_BEGIN
@interface KBShopItemVC : UIViewController<JXPagerViewListViewDelegate> @interface KBShopItemVC : UIViewController<JXPagerViewListViewDelegate>
@property (nonatomic, strong) UITableView *tableView; /// 列表:使用 UICollectionView 展示两列皮肤卡片
@property (nonatomic, strong) NSMutableArray <NSString *> *dataSource; @property (nonatomic, strong) UICollectionView *collectionView;
/// 数据源:简单字符串作为标题(演示用)
@property (nonatomic, strong) NSMutableArray<NSString *> *dataSource;
/// 是否需要上拉加载更多
@property (nonatomic, assign) BOOL isNeedFooter; @property (nonatomic, assign) BOOL isNeedFooter;
/// 是否需要下拉刷新
@property (nonatomic, assign) BOOL isNeedHeader; @property (nonatomic, assign) BOOL isNeedHeader;
@property (nonatomic, assign) BOOL isHeaderRefreshed; //默认为YES /// 首次是否已刷新过(避免重复触发)
@property (nonatomic, assign) BOOL isHeaderRefreshed; // 默认为 YES
@end @end
NS_ASSUME_NONNULL_END NS_ASSUME_NONNULL_END

View File

@@ -7,59 +7,55 @@
#import "KBShopItemVC.h" #import "KBShopItemVC.h"
#import <MJRefresh/MJRefresh.h> #import <MJRefresh/MJRefresh.h>
@interface KBShopItemVC ()<UITableViewDataSource, UITableViewDelegate> #import <Masonry/Masonry.h>
#import "KBSkinCardCell.h"
/// 使 UICollectionView KBSkinCardCell
@interface KBShopItemVC ()<UICollectionViewDataSource, UICollectionViewDelegateFlowLayout>
@property (nonatomic, copy) void(^scrollCallback)(UIScrollView *scrollView); @property (nonatomic, copy) void(^scrollCallback)(UIScrollView *scrollView);
@end @end
@implementation KBShopItemVC @implementation KBShopItemVC
- (void)viewDidLoad { - (void)viewDidLoad {
[super viewDidLoad]; [super viewDidLoad];
self.view.backgroundColor = [UIColor whiteColor];
_tableView = [[UITableView alloc] initWithFrame:CGRectZero style:UITableViewStylePlain]; // collectionView
self.tableView.backgroundColor = [UIColor whiteColor]; [self.view addSubview:self.collectionView];
self.tableView.tableFooterView = [UIView new]; [self.collectionView mas_makeConstraints:^(MASConstraintMaker *make) {
self.tableView.dataSource = self; make.edges.equalTo(self.view); // mas
self.tableView.delegate = self; }];
[self.tableView registerClass:[UITableViewCell class] forCellReuseIdentifier:@"cell"];
[self.view addSubview:self.tableView];
__weak typeof(self)weakSelf = self; // 2
__weak typeof(self) weakSelf = self;
if (self.isNeedHeader) { if (self.isNeedHeader) {
self.tableView.mj_header = [MJRefreshNormalHeader headerWithRefreshingBlock:^{ self.collectionView.mj_header = [MJRefreshNormalHeader headerWithRefreshingBlock:^{
dispatch_after(dispatch_time(DISPATCH_TIME_NOW, (int64_t)(2 * NSEC_PER_SEC)), dispatch_get_main_queue(), ^{ dispatch_after(dispatch_time(DISPATCH_TIME_NOW, (int64_t)(2 * NSEC_PER_SEC)), dispatch_get_main_queue(), ^{
[weakSelf.tableView.mj_header endRefreshing]; [weakSelf.collectionView.mj_header endRefreshing];
}); });
}]; }];
} }
if (self.isNeedFooter) { if (self.isNeedFooter) {
self.tableView.mj_footer = [MJRefreshAutoNormalFooter footerWithRefreshingBlock:^{ self.collectionView.mj_footer = [MJRefreshAutoNormalFooter footerWithRefreshingBlock:^{
dispatch_after(dispatch_time(DISPATCH_TIME_NOW, (int64_t)(2 * NSEC_PER_SEC)), dispatch_get_main_queue(), ^{ dispatch_after(dispatch_time(DISPATCH_TIME_NOW, (int64_t)(2 * NSEC_PER_SEC)), dispatch_get_main_queue(), ^{
[weakSelf.dataSource addObject:@"加载更多成功"]; [weakSelf.dataSource addObject:@"加载更多成功"]; //
[weakSelf.tableView reloadData]; [weakSelf.collectionView reloadData];
[weakSelf.tableView.mj_footer endRefreshing]; [weakSelf.collectionView.mj_footer endRefreshing];
}); });
}]; }];
if (@available(iOS 11.0, *)) { }
self.tableView.contentInsetAdjustmentBehavior = UIScrollViewContentInsetAdjustmentNever;
} if (@available(iOS 11.0, *)) {
} else { self.collectionView.contentInsetAdjustmentBehavior = UIScrollViewContentInsetAdjustmentNever;
//contentInsetAdjustmentBehaviorinset
// self.tableView.contentInset = UIEdgeInsetsMake(0, 0, UIApplication.sharedApplication.keyWindow.jx_layoutInsets.bottom, 0);
} }
[self beginFirstRefresh]; [self beginFirstRefresh];
} }
- (void)viewDidLayoutSubviews { #pragma mark -
[super viewDidLayoutSubviews];
self.tableView.frame = self.view.bounds;
}
//
- (void)beginFirstRefresh { - (void)beginFirstRefresh {
if (!self.isHeaderRefreshed) { if (!self.isHeaderRefreshed) {
[self beginRefreshImmediately]; [self beginRefreshImmediately];
@@ -68,74 +64,83 @@
- (void)beginRefreshImmediately { - (void)beginRefreshImmediately {
if (self.isNeedHeader) { if (self.isNeedHeader) {
[self.tableView.mj_header beginRefreshing]; [self.collectionView.mj_header beginRefreshing];
dispatch_after(dispatch_time(DISPATCH_TIME_NOW, (int64_t)(2 * NSEC_PER_SEC)), dispatch_get_main_queue(), ^{ dispatch_after(dispatch_time(DISPATCH_TIME_NOW, (int64_t)(2 * NSEC_PER_SEC)), dispatch_get_main_queue(), ^{
self.isHeaderRefreshed = YES; self.isHeaderRefreshed = YES;
[self.tableView reloadData]; [self.collectionView reloadData];
[self.tableView.mj_header endRefreshing]; [self.collectionView.mj_header endRefreshing];
}); });
}else { } else {
self.isHeaderRefreshed = YES; self.isHeaderRefreshed = YES;
[self.tableView reloadData]; [self.collectionView reloadData];
} }
} }
#pragma mark - UITableViewDataSource, UITableViewDelegate #pragma mark - UICollectionView DataSource
- (NSInteger)tableView:(UITableView *)tableView numberOfRowsInSection:(NSInteger)section { - (NSInteger)collectionView:(UICollectionView *)collectionView numberOfItemsInSection:(NSInteger)section {
if (!self.isHeaderRefreshed) { if (!self.isHeaderRefreshed) return 0;
return 0;
}
return self.dataSource.count; return self.dataSource.count;
} }
- (UITableViewCell *)tableView:(UITableView *)tableView cellForRowAtIndexPath:(NSIndexPath *)indexPath { - (__kindof UICollectionViewCell *)collectionView:(UICollectionView *)collectionView cellForItemAtIndexPath:(NSIndexPath *)indexPath {
UITableViewCell *cell = [tableView dequeueReusableCellWithIdentifier:@"cell" forIndexPath:indexPath]; KBSkinCardCell *cell = [collectionView dequeueReusableCellWithReuseIdentifier:@"KBSkinCardCell" forIndexPath:indexPath];
cell.textLabel.text = self.dataSource[indexPath.row]; NSString *title = (indexPath.item < self.dataSource.count) ? self.dataSource[indexPath.item] : @"Dopamine";
[cell configWithTitle:title imageURL:nil price:@"20"]; // 20
return cell; return cell;
} }
- (CGFloat)tableView:(UITableView *)tableView heightForRowAtIndexPath:(NSIndexPath *)indexPath { #pragma mark - UICollectionView DelegateFlowLayout
return 50;
- (CGSize)collectionView:(UICollectionView *)collectionView layout:(UICollectionViewLayout *)collectionViewLayout sizeForItemAtIndexPath:(NSIndexPath *)indexPath {
// 16 12
CGFloat insetLR = 16.0;
CGFloat spacing = 12.0;
CGFloat contentW = collectionView.bounds.size.width - insetLR * 2;
CGFloat itemW = floor((contentW - spacing) / 2.0);
CGFloat itemH = itemW * 0.75 + 56; // KBSkinCardCell
return CGSizeMake(itemW, itemH);
} }
- (void)tableView:(UITableView *)tableView didSelectRowAtIndexPath:(NSIndexPath *)indexPath { - (UIEdgeInsets)collectionView:(UICollectionView *)collectionView layout:(UICollectionViewLayout *)collectionViewLayout insetForSectionAtIndex:(NSInteger)section {
// DetailViewController *detailVC = [[DetailViewController alloc] init]; return UIEdgeInsetsMake(12, 16, 12, 16);
// detailVC.infoString = self.dataSource[indexPath.row];
// [self.navigationController pushViewController:detailVC animated:YES];
} }
- (CGFloat)collectionView:(UICollectionView *)collectionView layout:(UICollectionViewLayout *)collectionViewLayout minimumLineSpacingForSectionAtIndex:(NSInteger)section {
return 12.0;
}
- (CGFloat)collectionView:(UICollectionView *)collectionView layout:(UICollectionViewLayout *)collectionViewLayout minimumInteritemSpacingForSectionAtIndex:(NSInteger)section {
return 12.0;
}
#pragma mark - UIScrollView Delegate
- (void)scrollViewDidScroll:(UIScrollView *)scrollView { - (void)scrollViewDidScroll:(UIScrollView *)scrollView {
!self.scrollCallback ?: self.scrollCallback(scrollView); !self.scrollCallback ?: self.scrollCallback(scrollView);
} }
#pragma mark - JXPagingViewListViewDelegate #pragma mark - JXPagingViewListViewDelegate
- (UIView *)listView { return self.view; }
- (UIScrollView *)listScrollView { return self.collectionView; }
- (void)listViewDidScrollCallback:(void (^)(UIScrollView *))callback { self.scrollCallback = callback; }
- (void)listWillAppear { NSLog(@"%@:%@", self.title, NSStringFromSelector(_cmd)); }
- (void)listDidAppear { NSLog(@"%@:%@", self.title, NSStringFromSelector(_cmd)); }
- (void)listWillDisappear { NSLog(@"%@:%@", self.title, NSStringFromSelector(_cmd)); }
- (void)listDidDisappear { NSLog(@"%@:%@", self.title, NSStringFromSelector(_cmd)); }
- (UIView *)listView { #pragma mark - Lazy
return self.view; - (UICollectionView *)collectionView {
if (!_collectionView) {
UICollectionViewFlowLayout *layout = [UICollectionViewFlowLayout new];
layout.scrollDirection = UICollectionViewScrollDirectionVertical;
_collectionView = [[UICollectionView alloc] initWithFrame:CGRectZero collectionViewLayout:layout];
_collectionView.backgroundColor = [UIColor whiteColor];
_collectionView.dataSource = self;
_collectionView.delegate = self;
// cell
[_collectionView registerClass:KBSkinCardCell.class forCellWithReuseIdentifier:@"KBSkinCardCell"]; //
}
return _collectionView;
} }
- (UIScrollView *)listScrollView {
return self.tableView;
}
- (void)listViewDidScrollCallback:(void (^)(UIScrollView *))callback {
self.scrollCallback = callback;
}
- (void)listWillAppear {
NSLog(@"%@:%@", self.title, NSStringFromSelector(_cmd));
}
- (void)listDidAppear {
NSLog(@"%@:%@", self.title, NSStringFromSelector(_cmd));
}
- (void)listWillDisappear {
NSLog(@"%@:%@", self.title, NSStringFromSelector(_cmd));
}
- (void)listDidDisappear {
NSLog(@"%@:%@", self.title, NSStringFromSelector(_cmd));
}
@end @end