This commit is contained in:
2025-11-09 21:41:35 +08:00
parent 2415e97c97
commit 5e1a1f540e
9 changed files with 289 additions and 38 deletions

View File

@@ -40,6 +40,22 @@ typedef void(^KBEmptyAction)(void);
/// 触发刷新空数据视图(若集成了 DZNEmptyDataSet 则调用其 reloadEmptyDataSet
- (void)kb_reloadEmptyDataSet;
/// 使用 LYEmptyView 在表格页快速挂载一套统一样式的空态视图(与 DZNEmptyDataSet 二选一)
/// 注意:调用该方法会自动关闭本类内置的 DZNEmptyDataSetuseEmptyDataSet = NO避免冲突。
/// - Parameters:
/// - image: 占位图(可空)
/// - title: 标题(默认“暂无数据”)
/// - detail: 描述(可空)
/// - buttonTitle: 按钮标题(可空,传空则不展示按钮)
/// - tapHandler: 点击空白区域回调(可空)
/// - buttonHandler: 按钮点击回调(可空)
- (void)kb_makeDefaultEmptyViewWithImage:(nullable UIImage *)image
title:(nullable NSString *)title
detail:(nullable NSString *)detail
buttonTitle:(nullable NSString *)buttonTitle
tapHandler:(nullable KBEmptyAction)tapHandler
buttonHandler:(nullable KBEmptyAction)buttonHandler;
@end
NS_ASSUME_NONNULL_END

View File

@@ -13,6 +13,15 @@
#define KB_HAS_DZN 0
#endif
// LYEmptyView Pod +
#if __has_include(<LYEmptyView/UIView+Empty.h>)
#import <LYEmptyView/UIView+Empty.h>
#import <LYEmptyView/LYEmptyView.h>
#define KB_HAS_LY 1
#else
#define KB_HAS_LY 0
#endif
@interface BaseTableView ()
#if KB_HAS_DZN
<DZNEmptyDataSetSource, DZNEmptyDataSetDelegate>
@@ -127,4 +136,66 @@
#endif
// MARK: - LYEmptyView DZN
- (void)kb_makeDefaultEmptyViewWithImage:(UIImage *)image
title:(NSString *)title
detail:(NSString *)detail
buttonTitle:(NSString *)buttonTitle
tapHandler:(KBEmptyAction)tapHandler
buttonHandler:(KBEmptyAction)buttonHandler {
#if KB_HAS_LY
// DZN
self.useEmptyDataSet = NO;
//
NSString *t = title ?: @"暂无数据";
LYEmptyView *ev = nil;
if (buttonTitle.length > 0) {
// block
__weak typeof(self) weakSelf = self;
ev = [LYEmptyView emptyActionViewWithImage:image
titleStr:t
detailStr:detail ?: @""
btnTitleStr:buttonTitle
btnClickBlock:^{ if (buttonHandler) buttonHandler(); [weakSelf setNeedsLayout]; }];
} else {
//
ev = [LYEmptyView emptyViewWithImage:image titleStr:t detailStr:detail ?: @""];
}
//
if (tapHandler) {
ev.tapEmptyViewBlock = ^{ tapHandler(); };
}
//
ev.emptyViewIsCompleteCoverSuperView = NO; //
ev.ignoreContentInset = YES; // inset/
ev.contentViewOffset = -10; //
ev.subViewMargin = 14; //
ev.titleLabFont = [UIFont systemFontOfSize:16 weight:UIFontWeightSemibold];
ev.titleLabTextColor = [UIColor colorWithWhite:0.45 alpha:1.0];
ev.detailLabFont = [UIFont systemFontOfSize:14];
ev.detailLabTextColor = [UIColor colorWithWhite:0.6 alpha:1.0];
ev.detailLabLineSpacing = 3;
ev.detailLabMaxLines = 2;
ev.actionBtnFont = [UIFont systemFontOfSize:15 weight:UIFontWeightSemibold];
ev.actionBtnCornerRadius = 6;
ev.actionBtnBorderWidth = 1;
ev.actionBtnBorderColor = [UIColor colorWithWhite:0.88 alpha:1.0];
ev.actionBtnTitleColor = [UIColor colorWithRed:0.22 green:0.49 blue:0.96 alpha:1.0];
ev.actionBtnBackGroundColor = [UIColor whiteColor];
ev.actionBtnHorizontalMargin = 20; // + padding*2
//
ev.autoShowEmptyView = NO;
self.ly_emptyView = (LYEmptyView *)ev;
#else
// LYEmptyView
(void)image; (void)title; (void)detail; (void)buttonTitle; (void)tapHandler; (void)buttonHandler;
#endif
}
@end

View File

@@ -0,0 +1,40 @@
//
// UIScrollView+KBEmptyView.h
// keyBoard
//
// 统一封装基于 LYEmptyView 的空态视图挂载方法,适用于 UITableView/UICollectionView。
// 注意:仅在对应页面已通过 CocoaPods 集成 LYEmptyView 时生效。
//
#import <UIKit/UIKit.h>
NS_ASSUME_NONNULL_BEGIN
typedef void(^KBEmptyAction)(void);
@interface UIScrollView (KBEmptyView)
/// 快速挂载一套统一样式的空态视图LYEmptyView
/// - Parameters:
/// - image: 占位图(可空)
/// - title: 标题(默认“暂无数据”)
/// - detail: 描述(可空)
/// - buttonTitle: 按钮标题(可空,传空则不显示按钮)
/// - tapHandler: 点击空白区域回调(可空)
/// - buttonHandler: 按钮点击回调(可空)
- (void)kb_makeDefaultEmptyViewWithImage:(nullable UIImage *)image
title:(nullable NSString *)title
detail:(nullable NSString *)detail
buttonTitle:(nullable NSString *)buttonTitle
tapHandler:(nullable KBEmptyAction)tapHandler
buttonHandler:(nullable KBEmptyAction)buttonHandler;
/// 便捷加载态控制:关闭自动显隐后,配合网络请求手动控制显示/隐藏
- (void)kb_setLYAutoShowEnabled:(BOOL)enabled; // YES=自动显隐NO=手动
- (void)kb_beginLoadingForEmpty; // 请求开始:临时隐藏 empty
- (void)kb_endLoadingForEmpty; // 请求结束:根据数据源显示/隐藏(需确保先 reloadData
@end
NS_ASSUME_NONNULL_END

View File

@@ -0,0 +1,90 @@
//
// UIScrollView+KBEmptyView.m
// keyBoard
//
#import "UIScrollView+KBEmptyView.h"
#if __has_include(<LYEmptyView/UIView+Empty.h>)
#import <LYEmptyView/UIView+Empty.h>
#import <LYEmptyView/LYEmptyView.h>
#define KB_HAS_LY 1
#else
#define KB_HAS_LY 0
#endif
@implementation UIScrollView (KBEmptyView)
- (void)kb_makeDefaultEmptyViewWithImage:(UIImage *)image
title:(NSString *)title
detail:(NSString *)detail
buttonTitle:(NSString *)buttonTitle
tapHandler:(KBEmptyAction)tapHandler
buttonHandler:(KBEmptyAction)buttonHandler {
#if KB_HAS_LY
NSString *t = title ?: @"暂无数据";
LYEmptyView *ev = nil;
if (buttonTitle.length > 0) {
__weak typeof(self) weakSelf = self;
ev = [LYEmptyView emptyActionViewWithImage:image
titleStr:t
detailStr:detail ?: @""
btnTitleStr:buttonTitle
btnClickBlock:^{ if (buttonHandler) buttonHandler(); [weakSelf setNeedsLayout]; }];
} else {
ev = [LYEmptyView emptyViewWithImage:image titleStr:t detailStr:detail ?: @""];
}
if (tapHandler) { ev.tapEmptyViewBlock = ^{ tapHandler(); }; }
//
ev.emptyViewIsCompleteCoverSuperView = NO;
ev.ignoreContentInset = YES;
ev.contentViewOffset = -10;
ev.subViewMargin = 14;
ev.titleLabFont = [UIFont systemFontOfSize:16 weight:UIFontWeightSemibold];
ev.titleLabTextColor = [UIColor colorWithWhite:0.45 alpha:1.0];
ev.detailLabFont = [UIFont systemFontOfSize:14];
ev.detailLabTextColor = [UIColor colorWithWhite:0.6 alpha:1.0];
ev.detailLabLineSpacing = 3;
ev.detailLabMaxLines = 2;
ev.actionBtnFont = [UIFont systemFontOfSize:15 weight:UIFontWeightSemibold];
ev.actionBtnCornerRadius = 6;
ev.actionBtnBorderWidth = 1;
ev.actionBtnBorderColor = [UIColor colorWithWhite:0.88 alpha:1.0];
ev.actionBtnTitleColor = [UIColor colorWithRed:0.22 green:0.49 blue:0.96 alpha:1.0];
ev.actionBtnBackGroundColor = [UIColor whiteColor];
ev.actionBtnHorizontalMargin = 20;
// begin/end
ev.autoShowEmptyView = NO;
self.ly_emptyView = ev;
#else
(void)image; (void)title; (void)detail; (void)buttonTitle; (void)tapHandler; (void)buttonHandler;
#endif
}
- (void)kb_setLYAutoShowEnabled:(BOOL)enabled {
#if KB_HAS_LY
self.ly_emptyView.autoShowEmptyView = enabled;
#else
(void)enabled;
#endif
}
- (void)kb_beginLoadingForEmpty {
#if KB_HAS_LY
[self ly_startLoading];
#endif
}
- (void)kb_endLoadingForEmpty {
#if KB_HAS_LY
[self ly_endLoading];
#endif
}
@end