123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254 |
- //
- // UITableView+AddForPlaceholder.m
- // TableViewPlaceholder
- //
- // Created by TengShuQiang on 2017/12/4.
- // Copyright © 2017年 TTeng. All rights reserved.
- //
- #import "UITableView+AddForPlaceholder.h"
- #import <objc/runtime.h>
- @implementation UITableView (AddForPlaceholder)
- - (void)dealloc {
- [[NSNotificationCenter defaultCenter] removeObserver:self];
- }
- void swizzMethod(SEL oriSel, SEL newSel) {
-
- Class class = [UITableView class];
- Method oriMethod = class_getInstanceMethod(class, oriSel);
- Method newMethod = class_getInstanceMethod(class, newSel);
-
- BOOL success = class_addMethod(class, oriSel, method_getImplementation(newMethod), method_getTypeEncoding(newMethod));
- if (success) {
- class_replaceMethod(class, newSel, method_getImplementation(oriMethod), method_getTypeEncoding(oriMethod));
- } else {
- method_exchangeImplementations(oriMethod, newMethod);
- }
- }
- + (void)load {
- SEL selectors[] = {
- @selector(reloadData),
- @selector(insertSections:withRowAnimation:),
- @selector(deleteSections:withRowAnimation:),
- @selector(reloadSections:withRowAnimation:),
- @selector(insertRowsAtIndexPaths:withRowAnimation:),
- @selector(deleteRowsAtIndexPaths:withRowAnimation:),
- @selector(reloadRowsAtIndexPaths:withRowAnimation:),
- };
-
- for (NSUInteger index = 0; index < sizeof(selectors) / sizeof(SEL); ++index) {
- SEL originalSelector = selectors[index];
- SEL swizzledSelector = NSSelectorFromString([@"tt_" stringByAppendingString:NSStringFromSelector(originalSelector)]);
- swizzMethod(originalSelector, swizzledSelector);
- }
- }
- - (void)tt_reloadData {
- [self tt_reloadData];
- [self showPlaceholderNotice];
- }
- - (void)tt_insertSections:(NSIndexSet *)sections withRowAnimation:(UITableViewRowAnimation)animation {
- [self tt_insertSections:sections withRowAnimation:animation];
- [self showPlaceholderNotice];
- }
- - (void)tt_deleteSections:(NSIndexSet *)sections withRowAnimation:(UITableViewRowAnimation)animation {
- [self tt_deleteSections:sections withRowAnimation:animation];
- [self showPlaceholderNotice];
- }
- - (void)tt_reloadSections:(NSIndexSet *)sections withRowAnimation:(UITableViewRowAnimation)animation {
- [self tt_reloadSections:sections withRowAnimation:animation];
- [self showPlaceholderNotice];
- }
- - (void)tt_insertRowsAtIndexPaths:(NSArray<NSIndexPath *> *)indexPaths withRowAnimation:(UITableViewRowAnimation)animation {
- [self tt_insertRowsAtIndexPaths:indexPaths withRowAnimation:animation];
- [self showPlaceholderNotice];
- }
- - (void)tt_deleteRowsAtIndexPaths:(NSArray<NSIndexPath *> *)indexPaths withRowAnimation:(UITableViewRowAnimation)animation {
- [self tt_deleteRowsAtIndexPaths:indexPaths withRowAnimation:animation];
- [self showPlaceholderNotice];
- }
- - (void)tt_reloadRowsAtIndexPaths:(NSArray<NSIndexPath *> *)indexPaths withRowAnimation:(UITableViewRowAnimation)animation {
- [self tt_reloadRowsAtIndexPaths:indexPaths withRowAnimation:animation];
- [self showPlaceholderNotice];
- }
- - (void)showPlaceholderNotice {
- if (self.showNoDataNotice) {
- NSInteger sectionCount = self.numberOfSections;
- NSInteger rowCount = 0;
- for (int i = 0; i < sectionCount; i++) {
- rowCount += [self.dataSource tableView:self numberOfRowsInSection:i];
- }
- if (rowCount == 0) {
- if (self.showNoDataView) {
- if (self.customNoDataView) {
- self.backgroundView = [self customNoDataView];
- } else
- self.backgroundView = [self tt_defaultNoDataView];
- }
- } else {
- self.backgroundView = [[UIView alloc] init];
- }
- }
- }
- - (UIView *)tt_defaultNoDataView {
-
- if (self.defaultNoDataView) {
- return self.defaultNoDataView;
- }
- self.defaultNoDataView = ({
- UIView *view = [[UIView alloc] initWithFrame:self.bounds];
- UITapGestureRecognizer *tap = [[UITapGestureRecognizer alloc] initWithTarget:self action:@selector(tt_tapDefalutNoDataView:)];
- [view addGestureRecognizer:tap];
-
- [view addSubview:self.defaultNoDataNoticeImageView];
- [view addSubview:self.defaultNoDataNoticeLabel];
-
- [self layoutDefaultView:view];
-
- [[NSNotificationCenter defaultCenter] addObserver:self selector:@selector(onDeviceOrientationChange:) name:UIDeviceOrientationDidChangeNotification object:nil];
-
- view;
- });
-
- return self.defaultNoDataView;
- }
- - (void)layoutDefaultView:(UIView *)defaultView {
-
- UIImageView *imageView = self.defaultNoDataNoticeImageView;
- UIImage *image = self.defaultNoDataImage ? : [UIImage imageNamed:@"UITableViewPlaceholder.bundle/TableViewNoData"];
- imageView.image = image;
- CGFloat X = (self.bounds.size.width - image.size.width - self.contentInset.left - self.contentInset.right) / 2;
- CGFloat Y = (self.bounds.size.height - image.size.height - self.contentInset.top - self.contentInset.bottom) / 2 - 20;
- imageView.frame = CGRectMake(X, Y+self.noDataImageOffsetY, image.size.width, image.size.height);
-
- // 提示语不用太长,不考虑换行的情况,也不计算文字的宽高了
- UILabel *label = self.defaultNoDataNoticeLabel;
- label.text = self.defaultNoDataText ? : label.text;
- label.frame = CGRectMake(0, imageView.frame.origin.y + imageView.bounds.size.height + 10, self.bounds.size.width, 30);
- }
- - (void)tt_tapDefalutNoDataView:(UITapGestureRecognizer *)tap {
-
- self.defaultNoDataViewDidClickBlock ? self.defaultNoDataViewDidClickBlock(self.defaultNoDataView) : nil;
- }
- #pragma mark - notifications
- - (void)onDeviceOrientationChange:(NSNotification *)noti {
- if (self.customNoDataView || !self.showNoDataNotice) {
- return;
- }
- [self layoutDefaultView:self.defaultNoDataView];
- }
- #pragma mark - setter && getter
- - (void)setShowNoDataNotice:(BOOL)showNoDataNotice {
- objc_setAssociatedObject(self, @selector(showNoDataNotice), @(showNoDataNotice), OBJC_ASSOCIATION_ASSIGN);
- }
- - (BOOL)showNoDataNotice {
- return objc_getAssociatedObject(self, _cmd) == nil ? YES : [objc_getAssociatedObject(self, _cmd) boolValue];
- }
- - (void)setDefaultNoDataViewDidClickBlock:(void (^)(UIView *))defaultNoDataViewDidClickBlock {
- self.showNoDataNotice = YES;
- objc_setAssociatedObject(self, @selector(defaultNoDataViewDidClickBlock), defaultNoDataViewDidClickBlock, OBJC_ASSOCIATION_COPY_NONATOMIC);
- }
- - (void (^)(UIView *))defaultNoDataViewDidClickBlock {
- return objc_getAssociatedObject(self, _cmd);
- }
- - (void)setCustomNoDataView:(UIView *)customNoDataView {
- self.showNoDataNotice = YES;
- objc_setAssociatedObject(self, @selector(customNoDataView), customNoDataView, OBJC_ASSOCIATION_RETAIN_NONATOMIC);
- }
- - (UIView *)customNoDataView {
- return objc_getAssociatedObject(self, _cmd);
- }
- - (UIView *)defaultNoDataView {
- return objc_getAssociatedObject(self, _cmd);
- }
- - (void)setDefaultNoDataView:(UIView *)defaultNoDataView {
- objc_setAssociatedObject(self, @selector(defaultNoDataView), defaultNoDataView, OBJC_ASSOCIATION_RETAIN_NONATOMIC);
- }
- // 默认的label
- - (UILabel *)defaultNoDataNoticeLabel {
- UILabel *label = objc_getAssociatedObject(self, _cmd);
- if (!label) {
- label = [[UILabel alloc] init];
- label.text = self.defaultNoDataText ? : @"暂无数据,点击刷新";
- label.font = [UIFont systemFontOfSize:13];
- label.textColor = [UIColor lightGrayColor];
- label.textAlignment = NSTextAlignmentCenter;
- objc_setAssociatedObject(self, _cmd, label, OBJC_ASSOCIATION_RETAIN_NONATOMIC);
- }
- return label;
- }
- // 默认的imageView
- - (UIImageView *)defaultNoDataNoticeImageView {
- UIImageView *imageView = objc_getAssociatedObject(self, _cmd);
- if (!imageView) {
- imageView = [[UIImageView alloc] init];
- imageView.image = self.defaultNoDataImage ? : [UIImage imageNamed:@"UITableViewPlaceholder.bundle/TableViewNoData"];
- imageView.contentMode = UIViewContentModeCenter;
- objc_setAssociatedObject(self, _cmd, imageView, OBJC_ASSOCIATION_RETAIN_NONATOMIC);
- }
- return imageView;
- }
- - (NSString *)defaultNoDataText {
- return objc_getAssociatedObject(self, _cmd);
- }
- - (void)setDefaultNoDataText:(NSString *)defaultNoticeText {
- objc_setAssociatedObject(self, @selector(defaultNoDataText), defaultNoticeText, OBJC_ASSOCIATION_COPY_NONATOMIC);
- }
- - (UIImage *)defaultNoDataImage {
- return objc_getAssociatedObject(self, _cmd);
- }
- - (void)setDefaultNoDataImage:(UIImage *)defaultNoticeImage {
- objc_setAssociatedObject(self, @selector(defaultNoDataImage), defaultNoticeImage, OBJC_ASSOCIATION_RETAIN_NONATOMIC);
- }
- - (BOOL)showNoDataView {
- NSNumber *value = objc_getAssociatedObject(self, @selector(showNoDataView));
- return value.boolValue;
- }
- - (void)setShowNoDataView:(BOOL)showNoDataView {
- objc_setAssociatedObject(self, @selector(showNoDataView), @(showNoDataView), OBJC_ASSOCIATION_ASSIGN);
- }
- - (CGFloat)noDataImageOffsetY {
- NSNumber *value = objc_getAssociatedObject(self, @selector(noDataImageOffsetY));
- return value.floatValue;
- }
- - (void)setNoDataImageOffsetY:(CGFloat)noDataImageOffsetY {
- objc_setAssociatedObject(self, @selector(noDataImageOffsetY), @(noDataImageOffsetY), OBJC_ASSOCIATION_ASSIGN);
- }
- @end
|