Files
keyboard/keyBoard/Class/Base/V/BaseTableView.m

202 lines
7.3 KiB
Mathematica
Raw Normal View History

2025-10-29 14:28:57 +08:00
//
// BaseTableView.m
// keyBoard
//
#import "BaseTableView.h"
2025-10-29 14:49:35 +08:00
// DZNEmptyDataSet
#if __has_include(<DZNEmptyDataSet/UIScrollView+EmptyDataSet.h>)
#import <DZNEmptyDataSet/UIScrollView+EmptyDataSet.h>
#define KB_HAS_DZN 1
#else
#define KB_HAS_DZN 0
#endif
2025-11-09 21:41:35 +08:00
// 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
2025-10-29 14:49:35 +08:00
@interface BaseTableView ()
#if KB_HAS_DZN
<DZNEmptyDataSetSource, DZNEmptyDataSetDelegate>
#endif
@end
2025-10-29 14:28:57 +08:00
@implementation BaseTableView
- (instancetype)initWithFrame:(CGRect)frame style:(UITableViewStyle)style {
if (self = [super initWithFrame:frame style:style]) {
[self commonInit];
}
return self;
}
2025-10-29 14:49:35 +08:00
2025-10-29 14:28:57 +08:00
- (void)commonInit {
2025-10-29 14:49:35 +08:00
// iOS 15+
2025-10-29 14:28:57 +08:00
self.sectionHeaderTopPadding = 0;
self.backgroundColor = [UIColor whiteColor];
self.separatorStyle = UITableViewCellSeparatorStyleSingleLine;
self.estimatedRowHeight = 0;
self.estimatedSectionHeaderHeight = 0;
self.estimatedSectionFooterHeight = 0;
self.keyboardDismissMode = UIScrollViewKeyboardDismissModeOnDrag;
2025-10-29 14:49:35 +08:00
//
_useEmptyDataSet = YES; //
_emptyShouldAllowScroll = YES; //
_emptyVerticalOffset = 0; //
2025-11-17 20:07:39 +08:00
_emptyTitleText = KBLocalized(@"暂无数据"); //
2025-10-29 14:49:35 +08:00
#if KB_HAS_DZN
self.emptyDataSetSource = self;
self.emptyDataSetDelegate = self;
#endif
2025-10-29 14:28:57 +08:00
}
2025-10-29 14:49:35 +08:00
#pragma mark - Public
- (void)kb_reloadEmptyDataSet {
#if KB_HAS_DZN
[self reloadEmptyDataSet];
#endif
}
- (void)setUseEmptyDataSet:(BOOL)useEmptyDataSet {
_useEmptyDataSet = useEmptyDataSet;
#if KB_HAS_DZN
// /
self.emptyDataSetSource = useEmptyDataSet ? (id<DZNEmptyDataSetSource>)self : nil;
self.emptyDataSetDelegate = useEmptyDataSet ? (id<DZNEmptyDataSetDelegate>)self : nil;
[self reloadEmptyDataSet];
#endif
}
#pragma mark - DZNEmptyDataSet ()
#if KB_HAS_DZN
//
- (BOOL)emptyDataSetShouldDisplay:(UIScrollView *)scrollView {
return self.useEmptyDataSet;
}
//
- (NSAttributedString *)titleForEmptyDataSet:(UIScrollView *)scrollView {
NSString *title = self.emptyTitleText ?: @"";
NSDictionary *attrs = @{ NSFontAttributeName : [UIFont systemFontOfSize:16 weight:UIFontWeightSemibold],
NSForegroundColorAttributeName : [UIColor colorWithWhite:0.5 alpha:1.0] };
return [[NSAttributedString alloc] initWithString:title attributes:attrs];
}
2025-10-29 14:28:57 +08:00
2025-10-29 14:49:35 +08:00
//
- (NSAttributedString *)descriptionForEmptyDataSet:(UIScrollView *)scrollView {
if (self.emptyDescriptionText.length == 0) return nil;
NSDictionary *attrs = @{ NSFontAttributeName : [UIFont systemFontOfSize:14],
NSForegroundColorAttributeName : [UIColor colorWithWhite:0.6 alpha:1.0] };
return [[NSAttributedString alloc] initWithString:self.emptyDescriptionText attributes:attrs];
}
//
- (UIImage *)imageForEmptyDataSet:(UIScrollView *)scrollView {
return self.emptyImage;
}
//
- (NSAttributedString *)buttonTitleForEmptyDataSet:(UIScrollView *)scrollView forState:(UIControlState)state {
if (self.emptyButtonTitle.length == 0) return nil;
UIColor *color = (state == UIControlStateHighlighted) ? [UIColor darkTextColor] : [UIColor colorWithRed:0.22 green:0.49 blue:0.96 alpha:1.0];
NSDictionary *attrs = @{ NSFontAttributeName : [UIFont systemFontOfSize:15 weight:UIFontWeightSemibold],
NSForegroundColorAttributeName : color };
return [[NSAttributedString alloc] initWithString:self.emptyButtonTitle attributes:attrs];
}
//
- (CGFloat)verticalOffsetForEmptyDataSet:(UIScrollView *)scrollView {
return self.emptyVerticalOffset;
}
//
- (BOOL)emptyDataSetShouldAllowScroll:(UIScrollView *)scrollView { return self.emptyShouldAllowScroll; }
- (BOOL)emptyDataSetShouldAllowTouch:(UIScrollView *)scrollView { return YES; }
- (void)emptyDataSet:(UIScrollView *)scrollView didTapView:(UIView *)view {
if (self.emptyDidTapView) self.emptyDidTapView();
}
- (void)emptyDataSet:(UIScrollView *)scrollView didTapButton:(UIButton *)button {
if (self.emptyDidTapButton) self.emptyDidTapButton();
}
#endif
2025-11-09 21:41:35 +08:00
// 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;
//
2025-11-17 20:07:39 +08:00
NSString *t = title ?: KBLocalized(@"暂无数据");
2025-11-09 21:41:35 +08:00
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
}
2025-10-29 14:49:35 +08:00
@end