diff --git a/LYEmptyViewDemo.xcodeproj/project.pbxproj b/LYEmptyViewDemo.xcodeproj/project.pbxproj index 25a0896..e466dbc 100644 --- a/LYEmptyViewDemo.xcodeproj/project.pbxproj +++ b/LYEmptyViewDemo.xcodeproj/project.pbxproj @@ -9,7 +9,6 @@ /* Begin PBXBuildFile section */ B62151151FE0D13E00E8BB36 /* MBProgressHUD.m in Sources */ = {isa = PBXBuildFile; fileRef = B62151141FE0D13E00E8BB36 /* MBProgressHUD.m */; }; B62151341FE0D14E00E8BB36 /* Demo4ViewController.m in Sources */ = {isa = PBXBuildFile; fileRef = B621511F1FE0D14E00E8BB36 /* Demo4ViewController.m */; }; - B62151351FE0D14E00E8BB36 /* Demo4ViewController.xib in Resources */ = {isa = PBXBuildFile; fileRef = B62151201FE0D14E00E8BB36 /* Demo4ViewController.xib */; }; B62151361FE0D14E00E8BB36 /* DemoEmptyView.m in Sources */ = {isa = PBXBuildFile; fileRef = B62151221FE0D14E00E8BB36 /* DemoEmptyView.m */; }; B62151391FE0D14E00E8BB36 /* MyDIYEmpty.m in Sources */ = {isa = PBXBuildFile; fileRef = B62151281FE0D14E00E8BB36 /* MyDIYEmpty.m */; }; B621513A1FE0D14E00E8BB36 /* MyDIYViewController.m in Sources */ = {isa = PBXBuildFile; fileRef = B621512A1FE0D14E00E8BB36 /* MyDIYViewController.m */; }; @@ -62,7 +61,6 @@ B62151141FE0D13E00E8BB36 /* MBProgressHUD.m */ = {isa = PBXFileReference; fileEncoding = 4; lastKnownFileType = sourcecode.c.objc; path = MBProgressHUD.m; sourceTree = ""; }; B621511E1FE0D14E00E8BB36 /* Demo4ViewController.h */ = {isa = PBXFileReference; fileEncoding = 4; lastKnownFileType = sourcecode.c.h; path = Demo4ViewController.h; sourceTree = ""; }; B621511F1FE0D14E00E8BB36 /* Demo4ViewController.m */ = {isa = PBXFileReference; fileEncoding = 4; lastKnownFileType = sourcecode.c.objc; path = Demo4ViewController.m; sourceTree = ""; }; - B62151201FE0D14E00E8BB36 /* Demo4ViewController.xib */ = {isa = PBXFileReference; fileEncoding = 4; lastKnownFileType = file.xib; path = Demo4ViewController.xib; sourceTree = ""; }; B62151211FE0D14E00E8BB36 /* DemoEmptyView.h */ = {isa = PBXFileReference; fileEncoding = 4; lastKnownFileType = sourcecode.c.h; path = DemoEmptyView.h; sourceTree = ""; }; B62151221FE0D14E00E8BB36 /* DemoEmptyView.m */ = {isa = PBXFileReference; fileEncoding = 4; lastKnownFileType = sourcecode.c.objc; path = DemoEmptyView.m; sourceTree = ""; }; B62151271FE0D14E00E8BB36 /* MyDIYEmpty.h */ = {isa = PBXFileReference; fileEncoding = 4; lastKnownFileType = sourcecode.c.h; path = MyDIYEmpty.h; sourceTree = ""; }; @@ -189,7 +187,6 @@ B6ED70122251E266007C72DF /* Demo3ViewController.m */, B621511E1FE0D14E00E8BB36 /* Demo4ViewController.h */, B621511F1FE0D14E00E8BB36 /* Demo4ViewController.m */, - B62151201FE0D14E00E8BB36 /* Demo4ViewController.xib */, B649A86720A3D60C005F733C /* Demo5ViewController.h */, B649A86820A3D60C005F733C /* Demo5ViewController.m */, B65504BA211034D300F23218 /* Demo6ViewController.h */, @@ -417,7 +414,6 @@ B621513D1FE0D14E00E8BB36 /* OtherAppViewController.xib in Resources */, B621513B1FE0D14E00E8BB36 /* MyDIYViewController.xib in Resources */, B6E4CF89224B6190000E98AA /* BaseDemoViewController.xib in Resources */, - B62151351FE0D14E00E8BB36 /* Demo4ViewController.xib in Resources */, B6BEB1091ED9191400901EEB /* HomeListViewController.xib in Resources */, ); runOnlyForDeploymentPostprocessing = 0; diff --git a/LYEmptyViewDemo/Classes/Demo/Demo3ViewController.m b/LYEmptyViewDemo/Classes/Demo/Demo3ViewController.m index 00888df..6d67816 100644 --- a/LYEmptyViewDemo/Classes/Demo/Demo3ViewController.m +++ b/LYEmptyViewDemo/Classes/Demo/Demo3ViewController.m @@ -58,6 +58,7 @@ - (void)loadTestData{ - (DemoEmptyView *)noDataView{ if (!_noDataView) { _noDataView = [DemoEmptyView diyEmptyView]; + //_noDataView.autoShowEmptyView = NO; //二次封装的DemoEmptyView内如果设置过了,此处也可不写 } return _noDataView; } @@ -65,6 +66,7 @@ - (DemoEmptyView *)noDataView{ - (DemoEmptyView *)noNetworkView{ if (!_noNetworkView) { _noNetworkView = [DemoEmptyView diyEmptyActionViewWithTarget:self action:@selector(loadTestData)]; + //_noNetworkView.autoShowEmptyView = NO; //二次封装的DemoEmptyView内如果设置过了,此处也可不写 } return _noNetworkView; } diff --git a/LYEmptyViewDemo/Classes/Demo/Demo4ViewController.h b/LYEmptyViewDemo/Classes/Demo/Demo4ViewController.h index 3971fbc..28e7297 100644 --- a/LYEmptyViewDemo/Classes/Demo/Demo4ViewController.h +++ b/LYEmptyViewDemo/Classes/Demo/Demo4ViewController.h @@ -6,8 +6,8 @@ // Copyright © 2017年 liyang. All rights reserved. // -#import +#import "BaseDemoViewController.h" -@interface Demo4ViewController : UIViewController +@interface Demo4ViewController : BaseDemoViewController @end diff --git a/LYEmptyViewDemo/Classes/Demo/Demo4ViewController.m b/LYEmptyViewDemo/Classes/Demo/Demo4ViewController.m index e5717e6..df29d8b 100644 --- a/LYEmptyViewDemo/Classes/Demo/Demo4ViewController.m +++ b/LYEmptyViewDemo/Classes/Demo/Demo4ViewController.m @@ -9,59 +9,100 @@ #import "Demo4ViewController.h" #import "DemoEmptyView.h" -@interface Demo4ViewController () - -@property (weak, nonatomic) IBOutlet UITableView *tableView; -@property (nonatomic, strong) NSMutableArray *dataArray; - -@property (nonatomic, assign) NSInteger index; - +@interface Demo4ViewController () @end @implementation Demo4ViewController -- (void)viewWillAppear:(BOOL)animated{ - [super viewWillAppear:animated]; - - self.navigationController.navigationBar.barTintColor = MainColor(247, 247, 247); - self.navigationController.navigationBar.tintColor = MainColor(70, 70, 70); - - [self.navigationController.navigationBar setTitleTextAttributes: @{NSFontAttributeName:[UIFont boldSystemFontOfSize:17],NSForegroundColorAttributeName:MainColor(70, 70, 70)}]; +- (id)initWithNibName:(NSString *)nibNameOrNil bundle:(NSBundle *)nibBundleOrNil +{ + self = [super initWithNibName:NSStringFromClass([self.superclass class]) bundle:nibBundleOrNil]; + return self; } - (void)viewDidLoad { [super viewDidLoad]; - self.view.backgroundColor = MainColor(247, 247, 247); - self.automaticallyAdjustsScrollViewInsets = NO; - - NSMutableArray *arr = [NSMutableArray arrayWithArray:@[@"数据-0", @"数据-0"]]; + NSMutableArray *arr = [NSMutableArray arrayWithArray:@[@"数据-0", @"数据-1"]]; NSMutableArray *arr1 = [NSMutableArray arrayWithArray:@[]]; self.dataArray = [NSMutableArray array]; [self.dataArray addObject:arr]; [self.dataArray addObject:arr1]; - [self setupUI]; - - [self setEmpty]; + [self setUI]; } -- (void)setupUI{ - CGFloat tabbarH = self.tabBarController.tabBar.frame.size.height; - self.tableView.frame = CGRectMake(0, kStatusBarHeight + 44, kMainScreenWidth, kMainScreenHeight - kStatusBarHeight - 44 - tabbarH); +- (void)setUI{ self.tableView.delegate = self; self.tableView.dataSource = self; - self.tableView.backgroundColor = MainColor(247, 247, 247); - if (@available(iOS 11.0, *)) { - self.tableView.contentInsetAdjustmentBehavior = UIScrollViewContentInsetAdjustmentNever; - } + self.tableView.contentInset = UIEdgeInsetsMake(64, 0, 0, 0); DemoEmptyView *emptyView = [DemoEmptyView diyEmptyView]; - emptyView.contentViewOffset = 50; - emptyView.imageSize = CGSizeMake(80, 80); + //emptyView.autoShowEmptyView = NO; //二次封装的DemoEmptyView内如果设置过了,此处也可不写 + emptyView.contentViewOffset = 50;//偏移 + emptyView.imageSize = CGSizeMake(70, 70);//设置图片大小 self.tableView.ly_emptyView = emptyView; } +//模拟网络请求 +- (void)requestDataWithFinish:(LYRequestFinish)finish{ + + dispatch_async(dispatch_get_global_queue(DISPATCH_QUEUE_PRIORITY_DEFAULT, 0), ^{ + + if (![self.dataArray[1] count]) { + sleep(1); + [self.dataArray[1] removeAllObjects]; + NSMutableArray *arr = [@[@"数据-1-0", @"数据-1-1", @"数据-1-2"] mutableCopy]; + [self.dataArray[1] addObjectsFromArray:arr]; + self.indexNumber = 3; + dispatch_async(dispatch_get_main_queue(), ^{ + if (finish) { + finish(); + } + }); + } else { + [self.dataArray[1] addObject:[NSString stringWithFormat:@"数据-1-%zd", self.indexNumber++]]; + dispatch_async(dispatch_get_main_queue(), ^{ + if (finish) { + finish(); + } + }); + } + }); +} + +- (void)loadDataWithFinish:(LYRequestFinish)finish{ + + [self.tableView ly_hideEmptyView]; //手动隐藏emptyView + + [MBProgressHUD showHUDAddedTo:self.view animated:YES]; + //发起网络请求 + [self requestDataWithFinish:^{ + [MBProgressHUD hideHUDForView:self.view animated:YES]; + [self.tableView reloadData]; + [self setEmpty];//手动去判断emptyView的显隐 + }]; +} + +- (void)clearData{ + [self.dataArray[1] removeAllObjects]; + [self.tableView reloadData]; + [self setEmpty];//手动去判断emptyView的显隐 +} + +- (void)setEmpty{ + //需要自己去判断有无数据,从而根据有无情况进行显示 + if (self.dataArray.count >= 2) { + if ([self.dataArray[1] count]) { + [self.tableView ly_hideEmptyView];//手动隐藏emptyView + }else{ + [self.tableView ly_showEmptyView];//手动显示emptyView + } + }else{ + [self.tableView ly_showEmptyView];//手动显示emptyView + } +} + #pragma mark - -------------- TableView DataSource ----------------- - (NSInteger)numberOfSectionsInTableView:(UITableView *)tableView{ return [self.dataArray count]; @@ -103,64 +144,6 @@ - (void)tableView:(UITableView *)tableView didSelectRowAtIndexPath:(NSIndexPath [tableView deselectRowAtIndexPath:indexPath animated:YES]; } -- (void)getData:(id)sender{ - [self requestData]; -} - -- (void)requestData{ - - [self.tableView ly_hideEmptyView]; - [MBProgressHUD showHUDAddedTo:self.view animated:YES]; - dispatch_async(dispatch_get_global_queue(DISPATCH_QUEUE_PRIORITY_DEFAULT, 0), ^{ - - sleep(1); - [self.dataArray[1] removeAllObjects]; - NSArray *arr = @[@"数据-1", @"数据-1"]; - [self.dataArray[1] addObjectsFromArray:arr]; - - dispatch_async(dispatch_get_main_queue(), ^{ - [MBProgressHUD hideHUDForView:self.view animated:YES]; - [self.tableView reloadData]; - [self setEmpty]; - }); - }); -} -- (IBAction)loadData:(id)sender { - [self requestData]; -} -- (IBAction)clearData:(id)sender { - [self.dataArray[1] removeAllObjects]; - [self.tableView reloadData]; - [self setEmpty]; -} - -- (void)setEmpty{ - //需要自己去判断有无数据,从而根据有无情况进行显示 - if (self.dataArray.count >= 2) { - if ([self.dataArray[1] count]) { - [self.tableView ly_hideEmptyView]; - }else{ - [self.tableView ly_showEmptyView]; - } - }else{ - [self.tableView ly_showEmptyView]; - } -} - -- (void)didReceiveMemoryWarning { - [super didReceiveMemoryWarning]; - // Dispose of any resources that can be recreated. -} - -/* -#pragma mark - Navigation - -// In a storyboard-based application, you will often want to do a little preparation before navigation -- (void)prepareForSegue:(UIStoryboardSegue *)segue sender:(id)sender { - // Get the new view controller using [segue destinationViewController]. - // Pass the selected object to the new view controller. -} -*/ @end diff --git a/LYEmptyViewDemo/Classes/Demo/Demo4ViewController.xib b/LYEmptyViewDemo/Classes/Demo/Demo4ViewController.xib deleted file mode 100644 index 8bc780e..0000000 --- a/LYEmptyViewDemo/Classes/Demo/Demo4ViewController.xib +++ /dev/null @@ -1,69 +0,0 @@ - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - -