KWMBasePageVC.m 12.9 KB
Newer Older
houweibin committed
1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 23 24 25 26 27 28 29 30 31 32 33 34 35 36 37 38 39 40 41 42 43 44 45 46 47 48 49 50 51 52 53 54 55 56 57 58 59 60 61 62 63 64 65 66 67 68 69 70 71 72 73 74 75 76 77 78 79 80 81 82 83 84 85 86 87 88 89 90 91 92 93 94 95 96 97 98 99 100 101 102 103 104 105 106 107 108 109 110 111 112 113 114 115 116 117 118 119 120 121 122 123 124 125 126 127 128 129 130 131 132 133 134 135 136 137 138 139 140 141 142 143 144 145 146 147 148 149 150 151 152 153 154 155 156 157 158 159 160 161 162 163 164 165 166 167 168 169 170 171 172 173 174 175 176 177 178 179 180 181 182 183 184 185 186 187 188 189 190 191 192 193 194 195 196 197 198 199 200 201 202 203 204 205 206 207 208 209 210 211 212 213 214 215 216 217 218 219 220 221 222 223 224 225 226 227 228 229 230 231 232 233 234 235 236 237 238 239 240 241 242 243 244 245 246 247 248 249 250 251 252 253 254 255 256 257 258 259 260 261 262 263 264 265 266 267 268 269 270 271 272 273 274 275 276 277 278 279 280 281 282 283 284 285 286 287 288 289 290 291 292 293 294 295 296 297 298 299 300 301 302 303 304 305 306 307 308 309 310 311 312 313 314 315 316 317 318 319 320 321 322 323 324 325 326 327 328 329 330 331 332 333 334 335 336 337 338 339 340 341 342 343 344 345 346 347 348 349 350 351 352 353 354 355 356 357 358 359 360 361 362 363 364 365 366 367 368 369 370 371 372 373 374 375 376 377 378 379 380 381 382 383 384 385 386 387 388 389 390 391 392 393 394 395 396 397 398 399 400 401 402 403 404 405 406 407 408 409 410 411 412 413 414 415
//
//  KWMBasePageVC.m
//  iForeseaLife
//
//  Created by kevin on 7/7/14.
//  Copyright (c) 2014 Kollway Mobile. All rights reserved.
//

#import "KWMBasePageVC.h"
#import "MJRefresh.h"
#import "UIColor+SAMAdditions.h"
#import "MMJRefreshNormalHeader.h"
#import "KWMLoadingHeader.h"


@implementation KWMBasePageVC{
    
    //view
    __weak UIView *weakEmptyView;
    BOOL isLoading;
}

- (NSMutableArray *)dataList {
    if(_dataList == nil){
        _dataList = [[NSMutableArray alloc] initWithCapacity:100];
    }
    return _dataList;
}

/**
 * 设置下拉,上拉刷新
 */
- (void)setupPullToRefresh {
    __weak UITableView *tableView = [self targetTableView];
    __weak KWMBasePageVC *weakSelf = self;
    
    KWMLoadingHeader *header = [KWMLoadingHeader headerWithRefreshingBlock:^{
        dispatch_after(dispatch_time(DISPATCH_TIME_NOW, (int64_t)(2.0 * NSEC_PER_SEC)), dispatch_get_main_queue(), ^{
            [weakSelf reLoadData];
        });
    }];
//    header.lastUpdatedTimeLabel.hidden = YES;
//    header.stateLabel.hidden = YES;


   /*
    NSMutableArray *mrefreshingImages = [NSMutableArray array];
    for (NSUInteger i = 1; i<=31; i++) {
        UIImage *image = [UIImage imageNamed:[NSString stringWithFormat:@"loading%zd", i]];
        [mrefreshingImages addObject:image];
    }
    NSArray *refreshArray  = mrefreshingImages.copy;

    
    NSMutableArray *mrefreshingImages2 = [NSMutableArray array];
    for (NSUInteger i = 31; i>=1; i--) {
        UIImage *image = [UIImage imageNamed:[NSString stringWithFormat:@"loading%zd", i]];
        [mrefreshingImages2 addObject:image];
    }
    for (NSUInteger i = 1; i<=31; i++) {
        UIImage *image = [UIImage imageNamed:[NSString stringWithFormat:@"loading%zd", i]];
        [mrefreshingImages2 addObject:image];
    }
    
    NSArray *refreshArray2  = mrefreshingImages2.copy;
    */
    
//    // 设置普通状态的动画图片
//    NSMutableArray *mRefreshingImages2 = [NSMutableArray array];
//    [mRefreshingImages2 addObject:[UIImage imageNamed:@"loading31"]];
//    [header setImages:mRefreshingImages2 forState:MJRefreshStatePulling];
//    
//    // 设置即将刷新状态的动画图片(一松开就会刷新的状态)
//   
//    [header setImages:refreshArray forState:MJRefreshStateIdle];
//    
//    // 设置正在刷新状态的动画图片
//    
//    [header setImages:refreshArray2 duration:refreshArray2.count * 0.02 forState:MJRefreshStateRefreshing];
//
    
    tableView.mj_header = header;
    tableView.mj_header.automaticallyChangeAlpha = YES;
    
    tableView.mj_footer = [self createFooter];
}

- (MJRefreshAutoNormalFooter *)createFooter {
    __weak KWMBasePageVC *weakSelf = self;
    
    MJRefreshAutoNormalFooter *footer = [MJRefreshAutoNormalFooter footerWithRefreshingBlock:^{
        dispatch_after(dispatch_time(DISPATCH_TIME_NOW, (int64_t)(2.0 * NSEC_PER_SEC)), dispatch_get_main_queue(), ^{
            [weakSelf loadData];
        });
    }];
    footer.hidden = YES;
    return footer;
}

- (void)viewDidLoad {
    [super viewDidLoad];
    UITableView *tbv = [self targetTableView];
    tbv.delegate = self;
    tbv.dataSource = self;
    
    self.hasNextPage = YES;
    self.totalCount = @0;
    self.pageSize = @0;
    self.currentPage = @0;
    [self setupPullToRefresh];
    [self reLoadData];
}

//移除footerview(主要是去掉当数据没有显示点击或上啦加载更多的文字)
- (void)removeFooterView {
    __weak UITableView *tableView = [self targetTableView];
    tableView.mj_footer = nil;
}

- (void)loadData {
    NSAssert(NO, @"子类必须重写该方法");
}


- (UITableView *)targetTableView {
    NSAssert(NO, @"子类必须重写该方法");
    return nil;
}


- (void)reLoadData {
    if ([self isLoading]) {
        return;
    }
    self.hasNextPage = YES;
    self.currentPage = @0;
    self.totalCount = @0;
    [self loadData];
}

- (BOOL)isReload {
    _isReload = (self.totalCount.integerValue == 0);
    return _isReload;
}

//- (BOOL)hasNextPage {
//    _hasNextPage = self.totalCount.integerValue <= 0
//    || self.pageSize.integerValue * self.currentPage.integerValue < self.totalCount.integerValue;
//    return _hasNextPage;
//}

//Override
- (BOOL)isLoading {
    return isLoading;
}

//Override
- (void)showLoading {
    isLoading = YES;
    
    //数据为空的时候显示Loading框
    if ([self.targetTableView respondsToSelector:@selector(numberOfSections)]) {
        NSInteger sectionCount = [self.targetTableView numberOfSections];
        for(NSInteger i = 0; i < sectionCount; i++){
            if([self.targetTableView numberOfRowsInSection:i] <= 0){
               [super showLoading];
            }
        }
    }else{
        if([self.targetTableView numberOfRowsInSection:0] <= 0){
           [super showLoading];
        }
    }
}

//Override
- (void)hideLoading {
    isLoading = NO;
    [super hideLoading];
    
    __weak UITableView *tableView = [self targetTableView];
    [tableView.mj_header endRefreshing];
    [tableView.mj_footer endRefreshing];
}


-(void)hideLoadingForWorkDetail{
    isLoading = NO;
    __weak UITableView *tableView = [self targetTableView];
    [tableView.mj_header endRefreshing];
    [tableView.mj_footer endRefreshing];
}

- (void)appendDataList:(NSArray<Optional> *)result setPage:(NSInteger)page{
    [self appendDataList:result setPage:page setType:0 setTips:@""];
}


-(void)appendDataList:(NSArray<Optional> *)result setPage:(NSInteger)page setType:(NSInteger)type setTips:(NSString *)tips{
    [self appendDataList:result setPage:page setType:type isShowEmptyView:YES setTips:tips];
}

-(void)appendDataList:(NSArray<Optional> *)result setPage:(NSInteger)page setType:(NSInteger)type isShowEmptyView:(BOOL)isShow setTips:(NSString *)tips{
    __weak UITableView *tableView = [self targetTableView];
    //如果加载的数据是第一页,删除原来的数据
    if(page == 1){
        self.isReload = NO;
        [self.dataList removeAllObjects];
    }
    
    self.currentPage = @(page);

    if (result!= nil && result.count > 0) {
        [self.dataList addObjectsFromArray:result];
    }else{
        [self removeFooterView];
    }
    
    if ([self hasNextPage]) {
        tableView.mj_footer.hidden = NO;
    }else{
        tableView.mj_footer.hidden = YES;
    }
    [tableView reloadData];
    
    if (self.dataList.count <= 0 && isShow) {
        [self showEmptyView:type setTips:tips];
    }else{
        [self hideEmptyView];
    }
}

- (void)showError:(NSError *)error setType:(NSInteger)type{
    if(type > 0){
//        [self.dataList removeAllObjects];
//        UITableView *tbv = [self targetTableView];
//        [self removeFooterView];
//        [tbv reloadData];
        if(self.dataList.count <= 0){
            if ([error.domain isEqualToString:NSURLErrorDomain]) {
                //网络链接失败,请重试
                [self showEmptyView:KWMNoNework setTips:@""];
            }else{
                [self showEmptyView:KWMLoadFailure setTips:@""];
            }
        }else{
            [super showError:error];
        }
        
    }else{
        [super showError:error];
    }
}

-(void)showEmptyView:(NSInteger)type setTips:(NSString *)tips{
    [self hideEmptyView];
    UITableView *tbv = [self targetTableView];
    tbv.backgroundColor = [UIColor clearColor];
    NSInteger headerHeight = tbv.tableHeaderView.frame.size.height;
    UIView *emptyView = [[UIView alloc] initWithFrame:CGRectMake(0, headerHeight, UI_SCREEN_WIDTH, CGRectGetHeight(tbv.frame) - headerHeight)];
    emptyView.backgroundColor = [UIColor clearColor];
    emptyView.layer.borderColor = [[UIColor blackColor]CGColor];
    UIView *childView;
    if(type == KWMTextTips){//文字提示
        childView = [self getTxtTipsView:emptyView];
    }else{
        childView = [self getHasImageView:emptyView setType:type setTips:tips];
    }
    [emptyView addSubview:childView];

    if(type == KWMTextTips){
        [self.view insertSubview:emptyView belowSubview:tbv];
    }else{
        [self.view insertSubview:emptyView aboveSubview:tbv];
    }
    
    weakEmptyView = emptyView;
    
    
}

-(UIView *)getTxtTipsView:(UIView *)emptyView{
    NSString *msg = NSLocalizedString(@"InfoEmptyData", @"InfoEmptyData");
    NSInteger labelY =  (emptyView.frame.size.height)/2;
    
    UILabel *label = [[UILabel alloc] initWithFrame:CGRectMake(0, labelY,UI_SCREEN_WIDTH, 35)];
    label.textAlignment = NSTextAlignmentCenter;
    label.textColor = [UIColor darkGrayColor];
    label.font = [UIFont systemFontOfSize:16.0f];
    label.text = msg;
    return label;
    
    
}

//有图片的提示view
-(UIView *)getHasImageView:(UIView *)emptyView setType:(NSInteger)type setTips:(NSString *)tips{
    NSInteger childViewY =  CGRectGetHeight(emptyView.frame)/2-130;//130是空view高度的一半
    NSInteger childViewX = CGRectGetWidth(emptyView.frame)/2-100;//100是空view宽度的一半
    NSString *firstMsg = @"";
    NSString *secondMsg = @"";
    
    UIView *childView = [[UIView alloc] initWithFrame:CGRectMake(childViewX, childViewY, 200, 250)];
    childView.backgroundColor = [UIColor clearColor];
    UIImageView *imageView = [[UIImageView alloc] initWithFrame:CGRectMake(30, 0, 140, 100)];
    imageView.clipsToBounds = YES;
    NSString *imageName = @"";
    if(type == KWMNoDataTips){//没有数据的
        firstMsg = @"暂时没有找到数据";
        if(tips != nil){
            firstMsg = [NSString stringWithFormat:@"暂时没有找到%@",tips];
        }
        imageName = @"img_nothing";
    }else if(type == KWMNoLike){
        firstMsg = @"你暂时还木有收到赞赏";
        imageName = @"img_empty_like";
    }else if(type == KWMNoNework){//没有网络的
        firstMsg = @"网络已断开噢";
        secondMsg = @"请检查手机网络连接";
        imageName = @"img_nonet";
    }else if(type == KWMLoadFailure){//加载失败的
        firstMsg = @"网络加载失败噢";
        imageName = @"img_slownet";
    }else if(type == KWMNoComment){//没有评论的
        firstMsg = @"暂时还木有收到评论噢";
        imageName = @"img_nocomment";
    }else if(type == KWMNoFans){//没有关注的
        firstMsg = @"你暂时还没有朋友关注哦";
        imageName = @"img_unlogin";
    }else if(type == KWMNoCollect){
        firstMsg = @"你作品暂时还木有被收藏";
        imageName = @"img_nocollect";
    }
    [imageView setImage:[UIImage imageNamed:imageName]];
    imageView.contentMode = UIViewContentModeScaleAspectFit;
    [childView addSubview:imageView];
    UILabel *lbFirst = [[UILabel alloc] initWithFrame:CGRectMake(0, 110,200, 20)];
    lbFirst.textAlignment = NSTextAlignmentCenter;
    lbFirst.textColor = [UIColor sam_colorWithHex:@"666666"];
    lbFirst.font = [UIFont systemFontOfSize:13.0f];
    lbFirst.text = firstMsg;
    [childView addSubview:lbFirst];
    UILabel *lbSecond = [[UILabel alloc] initWithFrame:CGRectMake(0, 140,200, 21)];
    lbSecond.textAlignment = NSTextAlignmentCenter;
    lbSecond.textColor = [UIColor sam_colorWithHex:@"666666"];
    lbSecond.font = [UIFont systemFontOfSize:13.0f];
    lbSecond.text = secondMsg;
    [childView addSubview:lbSecond];
    NSString *btnMsg = @"点击刷新";
    UIButton *btn = [[UIButton alloc] initWithFrame:CGRectMake(10, 170, 180, 40)];
    btn.backgroundColor = [UIColor clearColor];//FDA03D
    btn.clipsToBounds = YES;
    btn.userInteractionEnabled = YES;
    btn.layer.borderWidth = 1;
    btn.layer.borderColor = [[UIColor sam_colorWithHex:@"FDA03D"] CGColor];
    btn.layer.cornerRadius = 20;
    [btn setTintColor:[UIColor whiteColor]];
    [btn setTitleColor:[UIColor sam_colorWithHex:@"FDA03D"] forState:UIControlStateNormal];
    btn.titleLabel.font = [UIFont systemFontOfSize: 14.0];
    [btn setTitle:btnMsg forState:UIControlStateNormal];
    [btn addTarget:self action:@selector(onClickEmptyViewBtn) forControlEvents:UIControlEventTouchUpInside];
    [childView addSubview:btn];
    return childView;
}

//点击按钮的
-(void)onClickEmptyViewBtn{
    [self reLoadData];
}

-(void)hideEmptyView{
    if(weakEmptyView != nil){
        weakEmptyView.hidden = YES;
        [weakEmptyView removeFromSuperview];
        weakEmptyView = nil;
    }
}


#pragma mark - EGORefreshTableDelegate

- (void)egoRefreshTableDidTriggerRefresh:(EGORefreshPos)aRefreshPos {
    switch (aRefreshPos){
        case EGORefreshHeader:{
            [self reLoadData];
            break;
        }
        case EGORefreshFooter:{
            [self loadData];
            break;
        }
    }
}

- (BOOL)egoRefreshTableDataSourceIsLoading:(UIView *)view {
    return [self isLoading];
}

- (NSDate *)egoRefreshTableDataSourceLastUpdated:(UIView *)view {
    return [NSDate date];
}

#pragma mark UITableViewDataSource

- (NSInteger)tableView:(UITableView *)tableView numberOfRowsInSection:(NSInteger)section {
    NSAssert(NO, @"子类必须重写该方法");
    return 0;
}

- (UITableViewCell *)tableView:(UITableView *)tableView cellForRowAtIndexPath:(NSIndexPath *)indexPath {
    NSAssert(NO, @"子类必须重写该方法");
    return nil;
}

@end