// // Copyright © 2016年 Anson. All rights reserved. // #import "UIScrollView+Empty.h" #import #import static NSString * LCUIScrollViewEmptyKVOContext = @"LCUIScrollViewEmptyKVOContext"; @implementation UIScrollView (Empty) static const void *IndieEmptyViewKey = &IndieEmptyViewKey; #pragma mark - getter && setter - (void)setLc_emptyImage:(UIImage *)lc_emptyImage { UIImageView *imageView = [[UIImageView alloc]initWithFrame:self.bounds]; imageView.image = lc_emptyImage; imageView.autoresizingMask = UIViewAutoresizingFlexibleWidth | UIViewAutoresizingFlexibleHeight; imageView.contentMode = UIViewContentModeCenter; self.lc_emptyView = imageView; } - (UIImage *)lc_emptyImage { if (self.lc_emptyView!=nil && [self.lc_emptyView isKindOfClass:[UIImageView class]]) { UIImageView *imageView = (UIImageView *)self.lc_emptyView; return imageView.image; } return nil; } - (void)setLc_emptyView:(UIView *)lc_emptyView { lc_emptyView.hidden = self.lc_emptyView.hidden; if (self.lc_emptyView!=nil) { [self.lc_emptyView removeFromSuperview]; } [self addSubview:lc_emptyView]; lc_emptyView.autoresizingMask = UIViewAutoresizingFlexibleWidth | UIViewAutoresizingFlexibleHeight; [self bringSubviewToFront:lc_emptyView]; __weak UIScrollView *weakSelf = self; [self.KVOController observe:self keyPath:@"contentSize" options:NSKeyValueObservingOptionNew block:^(id _Nullable observer, id _Nonnull object, NSDictionary * _Nonnull change) { [weakSelf contentSizeChange]; }]; objc_setAssociatedObject(self, IndieEmptyViewKey, lc_emptyView, OBJC_ASSOCIATION_RETAIN_NONATOMIC); } - (UIView *)lc_emptyView { UIView *emptyView = objc_getAssociatedObject(self, IndieEmptyViewKey); return emptyView; } #pragma mark - - (void)contentSizeChange { UIView *emptyView = self.lc_emptyView; if (emptyView==nil) { return ; } //当UITbleView时 计算出row的总行数 进行判断 if ([self isKindOfClass:[UITableView class]]) { NSLog(@"%@", @"contentSize change----"); UITableView *tv = (UITableView *)self; if(tv.dataSource == nil) { return ; } if (![tv.dataSource respondsToSelector:@selector(tableView:numberOfRowsInSection:)]) { return; } //默认section数为1 有委托用委托中的值 NSInteger countOfSection = 1; if ([tv.dataSource respondsToSelector:@selector(numberOfSectionsInTableView:)]) { countOfSection = [tv.dataSource numberOfSectionsInTableView:tv]; } NSInteger allCountOfRow = 0; for (NSInteger i=0; i