BaseListViewController.m 4.63 KB
Newer Older
1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17
//
//  BaseListViewController.m
//  RealEstateManagement
//
//  Created by Javen on 16/9/8.
//  Copyright © 2016年 上海勾芒信息科技. All rights reserved.
//

#import "BaseListViewController.h"
@interface BaseListViewController ()

@end

@implementation BaseListViewController

#pragma mark - life cycle

Sandy's avatar
Sandy committed
18
- (void)viewDidLoad {
19 20 21 22 23 24 25 26 27 28 29 30 31 32
    [super viewDidLoad];

    self.pageSize = 15;
    self.tableView.delegate = self;
    self.tableView.dataSource = self;
    self.tableView.emptyDataSetSource = self;
    self.tableView.emptyDataSetDelegate = self;
    self.tableView.estimatedRowHeight = 159;
    self.tableView.rowHeight = UITableViewAutomaticDimension;
    self.tableView.tableFooterView = [UIView new];
    [self paggingMode];
    // Do any additional setup after loading the view.
}

Sandy's avatar
Sandy committed
33
- (void)viewDidAppear:(BOOL)animated {
34 35 36
    [super viewDidAppear:animated];
}

Sandy's avatar
Sandy committed
37
- (void)viewWillAppear:(BOOL)animated {
38 39 40 41
    [super viewWillAppear:animated];
    [self.navigationController setNavigationBarHidden:NO animated:YES];
}

Sandy's avatar
Sandy committed
42
- (void)viewWillDisappear:(BOOL)animated {
43 44 45 46
    [super viewWillDisappear:animated];
    [self.navigationController setNavigationBarHidden:NO animated:YES];
}

Sandy's avatar
Sandy committed
47
- (void)didReceiveMemoryWarning {
48 49 50 51 52 53 54
    [super didReceiveMemoryWarning];
    // Dispose of any resources that can be recreated.
}

/**
 *  启用分页加载(最好在ViewDidLoad里面调用)
 */
Sandy's avatar
Sandy committed
55
- (void)paggingMode {
56 57 58 59 60 61 62 63 64 65 66 67 68 69 70
    WS(weakSelf);
    self.tableView.mj_header = [MJRefreshNormalHeader headerWithRefreshingBlock:^{
        weakSelf.page = 0;
        [weakSelf.arrData removeAllObjects];
        /**
         *  子类里面要重写httpRequest方法
         */
        [MBProgressHUD j_loading];
        [weakSelf httpRequest];
    }];

    self.tableView.mj_footer = [MJRefreshAutoNormalFooter footerWithRefreshingBlock:^{
        weakSelf.page++;
        [weakSelf httpRequest];
    }];
Sandy's avatar
Sandy committed
71
    self.tableView.mj_footer.automaticallyHidden = YES;
72 73
}

Sandy's avatar
Sandy committed
74
- (void)refresh {
75 76 77
    [self.tableView.mj_header beginRefreshing];
}

Sandy's avatar
Sandy committed
78
- (NSInteger)tableView:(UITableView *)tableView numberOfRowsInSection:(NSInteger)section {
79 80 81
    return self.arrData.count;
}

Sandy's avatar
Sandy committed
82
- (void)tableView:(UITableView *)tableView didSelectRowAtIndexPath:(NSIndexPath *)indexPath {
83 84 85 86 87
    [tableView deselectRowAtIndexPath:indexPath animated:YES];
    [self listDidSelect:self.arrData[indexPath.row]];
}

#pragma mark - empty state
Sandy's avatar
Sandy committed
88
- (UIImage *)imageForEmptyDataSet:(UIScrollView *)scrollView {
89 90 91
    return [UIImage imageNamed:@"list_no_data"];
}

Sandy's avatar
Sandy committed
92
- (void)emptyDataSet:(UIScrollView *)scrollView didTapView:(UIView *)view {
93 94
    [self.tableView.mj_header beginRefreshing];
}
Sandy's avatar
Sandy committed
95
- (CAAnimation *)imageAnimationForEmptyDataSet:(UIScrollView *)scrollView {
96 97 98 99 100 101 102 103 104 105
    CABasicAnimation *animation = [CABasicAnimation animationWithKeyPath:@"transform"];
    animation.fromValue = [NSValue valueWithCATransform3D:CATransform3DIdentity];
    animation.toValue = [NSValue valueWithCATransform3D:CATransform3DMakeRotation(M_PI_2, 0.0, 0.0, 1.0)];
    animation.duration = 1;
    animation.cumulative = YES;
    animation.repeatCount = MAXFLOAT;

    return animation;
}

Sandy's avatar
Sandy committed
106
- (UIColor *)backgroundColorForEmptyDataSet:(UIScrollView *)scrollView {
107 108 109
    return [UIColor whiteColor];
}

Sandy's avatar
Sandy committed
110
- (IBAction)actionAdd:(UIButton *)sender {
111 112
}

Sandy's avatar
Sandy committed
113
- (void)listTableViewReloadData {
114
    [MBProgressHUD j_hideLoadingView];
115
    
116
    //停止下拉刷新
117 118 119 120 121 122 123
    if (self.tableView.mj_header.isRefreshing) {
        [self.tableView.mj_header endRefreshing];
        if (self.arrData.count == self.pageSize) {
            [self.tableView.mj_footer resetNoMoreData];
        }else{
            [self.tableView.mj_footer endRefreshingWithNoMoreData];
        }
124
        //停止下拉加载
125 126
    }else if (self.tableView.mj_footer.isRefreshing){
        
Sandy's avatar
Sandy committed
127 128 129
        if (self.arrData.count < self.pageSize * self.page) {
            [self.tableView.mj_footer endRefreshingWithNoMoreData];
        } else {
130
            [self.tableView.mj_footer endRefreshing];
Sandy's avatar
Sandy committed
131
        }
Sandy's avatar
Sandy committed
132
    }
133
    
Sandy's avatar
Sandy committed
134
    [self.tableView reloadData];
135 136
}

Sandy's avatar
Sandy committed
137
- (void)listTableViewReloadDataWithNewRecord:(NSArray *)newRecord {
138 139 140 141 142 143
    [self listTableViewReloadData];
    CLog(@"newrecord count = %lu", newRecord.count);
    if ([newRecord count] < self.pageSize) {
        [self.tableView.mj_footer endRefreshingWithNoMoreData];
    }
}
Sandy's avatar
Sandy committed
144
- (NSMutableArray *)arrData {
145 146 147 148 149 150 151 152 153 154 155 156 157 158 159 160
    if (!_arrData) {
        _arrData = [NSMutableArray array];
    }
    return _arrData;
}
/*
#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