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

131 lines
4.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
@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; //
_emptyTitleText = @"暂无数据"; //
#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
@end