Skip to content

Commit

Permalink
demo4重构
Browse files Browse the repository at this point in the history
  • Loading branch information
dev-liyang committed Apr 1, 2019
1 parent 5bafb39 commit 2255afb
Show file tree
Hide file tree
Showing 5 changed files with 75 additions and 163 deletions.
4 changes: 0 additions & 4 deletions LYEmptyViewDemo.xcodeproj/project.pbxproj
Original file line number Diff line number Diff line change
Expand Up @@ -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 */; };
Expand Down Expand Up @@ -62,7 +61,6 @@
B62151141FE0D13E00E8BB36 /* MBProgressHUD.m */ = {isa = PBXFileReference; fileEncoding = 4; lastKnownFileType = sourcecode.c.objc; path = MBProgressHUD.m; sourceTree = "<group>"; };
B621511E1FE0D14E00E8BB36 /* Demo4ViewController.h */ = {isa = PBXFileReference; fileEncoding = 4; lastKnownFileType = sourcecode.c.h; path = Demo4ViewController.h; sourceTree = "<group>"; };
B621511F1FE0D14E00E8BB36 /* Demo4ViewController.m */ = {isa = PBXFileReference; fileEncoding = 4; lastKnownFileType = sourcecode.c.objc; path = Demo4ViewController.m; sourceTree = "<group>"; };
B62151201FE0D14E00E8BB36 /* Demo4ViewController.xib */ = {isa = PBXFileReference; fileEncoding = 4; lastKnownFileType = file.xib; path = Demo4ViewController.xib; sourceTree = "<group>"; };
B62151211FE0D14E00E8BB36 /* DemoEmptyView.h */ = {isa = PBXFileReference; fileEncoding = 4; lastKnownFileType = sourcecode.c.h; path = DemoEmptyView.h; sourceTree = "<group>"; };
B62151221FE0D14E00E8BB36 /* DemoEmptyView.m */ = {isa = PBXFileReference; fileEncoding = 4; lastKnownFileType = sourcecode.c.objc; path = DemoEmptyView.m; sourceTree = "<group>"; };
B62151271FE0D14E00E8BB36 /* MyDIYEmpty.h */ = {isa = PBXFileReference; fileEncoding = 4; lastKnownFileType = sourcecode.c.h; path = MyDIYEmpty.h; sourceTree = "<group>"; };
Expand Down Expand Up @@ -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 */,
Expand Down Expand Up @@ -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;
Expand Down
2 changes: 2 additions & 0 deletions LYEmptyViewDemo/Classes/Demo/Demo3ViewController.m
Original file line number Diff line number Diff line change
Expand Up @@ -58,13 +58,15 @@ - (void)loadTestData{
- (DemoEmptyView *)noDataView{
if (!_noDataView) {
_noDataView = [DemoEmptyView diyEmptyView];
//_noDataView.autoShowEmptyView = NO; //二次封装的DemoEmptyView内如果设置过了,此处也可不写
}
return _noDataView;
}

- (DemoEmptyView *)noNetworkView{
if (!_noNetworkView) {
_noNetworkView = [DemoEmptyView diyEmptyActionViewWithTarget:self action:@selector(loadTestData)];
//_noNetworkView.autoShowEmptyView = NO; //二次封装的DemoEmptyView内如果设置过了,此处也可不写
}
return _noNetworkView;
}
Expand Down
4 changes: 2 additions & 2 deletions LYEmptyViewDemo/Classes/Demo/Demo4ViewController.h
Original file line number Diff line number Diff line change
Expand Up @@ -6,8 +6,8 @@
// Copyright © 2017年 liyang. All rights reserved.
//

#import <UIKit/UIKit.h>
#import "BaseDemoViewController.h"

@interface Demo4ViewController : UIViewController
@interface Demo4ViewController : BaseDemoViewController

@end
159 changes: 71 additions & 88 deletions LYEmptyViewDemo/Classes/Demo/Demo4ViewController.m
Original file line number Diff line number Diff line change
Expand Up @@ -9,59 +9,100 @@
#import "Demo4ViewController.h"
#import "DemoEmptyView.h"

@interface Demo4ViewController ()<UITableViewDataSource, UITableViewDelegate>

@property (weak, nonatomic) IBOutlet UITableView *tableView;
@property (nonatomic, strong) NSMutableArray *dataArray;

@property (nonatomic, assign) NSInteger index;

@interface Demo4ViewController ()<UITableViewDelegate, UITableViewDataSource>
@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];
Expand Down Expand Up @@ -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
69 changes: 0 additions & 69 deletions LYEmptyViewDemo/Classes/Demo/Demo4ViewController.xib

This file was deleted.

0 comments on commit 2255afb

Please sign in to comment.