// // TPTableViewController.m // ThePaperDemo // // Created by scar1900 on 14/10/21. // Copyright (c) 2014年 scar1900. All rights reserved. // #import "TPTableViewController.h" #pragma mark - 控件的刷新状态 typedef enum { RefreshStatePulling = 1, // 松开就可以进行刷新的状态 RefreshStateNormal = 2, // 普通状态 RefreshStateRefreshing = 3, // 正在刷新中的状态 RefreshStateCancel = 4, RefreshStateSucess = 5, RefreshStateIsPulling = 6, RefreshStateNoMore = 7, RefreshStateFailed = 8 } RefreshState; @protocol LoadFootDelegate @optional - (void)startLoad;//开始刷新代理 @end @interface loadFootView() @property(nonatomic, strong)UIActivityIndicatorView *activityView; @property(nonatomic, assign)RefreshState state; @property(nonatomic, weak)id delegate; @property(nonatomic, strong)UILabel *textLabel; @end @implementation loadFootView @synthesize state = _state; @synthesize delegate; - (id)initWithFrame:(CGRect)frame { self = [super initWithFrame:frame]; if (self) { self.backgroundColor = [UIColor clearColor]; [self addSubview:self.activityView]; [self addSubview:self.textLabel]; self.state = RefreshStateNormal; } return self; } - (UIActivityIndicatorView*)activityView { if (!_activityView) { _activityView = [[UIActivityIndicatorView alloc]initWithActivityIndicatorStyle:UIActivityIndicatorViewStyleGray]; _activityView.frame = CGRectMake(CGRectGetWidth(self.bounds)/2-25/2, 10, 25, 25); [_activityView stopAnimating]; _activityView.hidesWhenStopped = YES; } return _activityView; } - (UILabel*)textLabel { if (!_textLabel) { _textLabel = [[UILabel alloc]initWithFrame:CGRectMake(0,CGRectGetMaxY(self.activityView.frame)+5 , CGRectGetWidth(self.bounds), 20)]; _textLabel.textAlignment = NSTextAlignmentCenter; _textLabel.textColor = [UIColor colorWithHexString:TextGray]; _textLabel.autoresizingMask = AutoresizingAuto; _textLabel.backgroundColor = [UIColor clearColor]; } _textLabel.font = appFont(11, NO); return _textLabel; } - (void)setState:(RefreshState )sta { if (_state == sta) return; self.activityView.frame = CGRectMake(CGRectGetWidth(self.bounds)/2-25/2, 10, 25, 25); _state = sta; if (sta == RefreshStateNormal) { [self.activityView stopAnimating]; self.textLabel.hidden = NO; self.textLabel.frame = self.bounds; self.textLabel.text = @"上拉加载数据"; }else if (sta == RefreshStateRefreshing) { self.textLabel.hidden = NO; [self.activityView startAnimating]; self.textLabel.frame = CGRectMake(0,CGRectGetMaxY(self.activityView.frame)+5 , CGRectGetWidth(self.bounds), 20); self.textLabel.text = @"正在加载"; if ([delegate conformsToProtocol:@protocol(LoadFootDelegate)] && [delegate respondsToSelector:@selector(startRefresh)]) { [delegate startLoad]; } }else if (sta == RefreshStateCancel) { self.state = RefreshStateNormal; }else if (sta == RefreshStateSucess) { self.state = RefreshStateNormal; }else if (sta == RefreshStateFailed) { // ShowTextMessage(@"加载失败"); self.state = RefreshStateNormal; }else if (sta == RefreshStateNoMore) { [self.activityView stopAnimating]; self.textLabel.hidden = NO; self.textLabel.frame = self.bounds; self.textLabel.text = @"没有更多数据"; }else if (sta == RefreshStatePulling) { [self.activityView stopAnimating]; self.textLabel.hidden = NO; self.textLabel.frame = self.bounds; self.textLabel.text = @"释放加载数据"; } } - (void)layoutSubviews { [super layoutSubviews]; if ([[TPUserDefault instance].isNightMode intValue] > 0) { self.activityView.activityIndicatorViewStyle = UIActivityIndicatorViewStyleWhite; }else { self.activityView.activityIndicatorViewStyle = UIActivityIndicatorViewStyleGray; } } @end @interface TPTableViewController () { BOOL isCanLoad; BOOL isCanRefresh; } @end @implementation TPTableViewController @synthesize tableView; @synthesize nextUrl = _nextUrl; @synthesize tableDelegate; - (id)init { if (self = [super init]) { CGFloat width = [UIScreen mainScreen].applicationFrame.size.width; CGFloat height = [UIScreen mainScreen].applicationFrame.size.height; self.tableView = [[HeaderInsetTableView alloc] initWithFrame:CGRectMake(0, 0, width, height) style:UITableViewStylePlain]; self.tableView.delegate = self; self.tableView.dataSource = self; self.tableView.autoresizingMask = AutoresizingFull; self.tableView.backgroundColor = [UIColor clearColor]; isCanLoad = NO; isCanRefresh = NO; } return self; } - (void)viewDidLoad { [super viewDidLoad]; // Do any additional setup after loading the view. self.tableView.frame = self.view.bounds; self.tableView.clipsToBounds = NO; [self.view addSubview:self.tableView]; } - (void)viewWillLayoutSubviews { [super viewWillLayoutSubviews]; if (isCanRefresh) { self.tableView.refreshControl.frame = CGRectMake(0, self.tableView.refreshControl.frame.origin.y, CGRectGetWidth(self.tableView.frame), self.tableView.refreshControl.frame.size.height); } } - (void)setLoadFooterFrame:(CGRect)frame { self.loadFooter.frame = frame; } - (loadFootView*)loadFooter { if (!_loadFooter) { _loadFooter = [[loadFootView alloc]initWithFrame:CGRectMake(0, CGRectGetHeight(self.tableView.bounds) ,CGRectGetWidth(self.view.bounds) , 60)]; _loadFooter.hidden = YES; _loadFooter.delegate = self; } return _loadFooter; } - (void)configCanRefresh:(BOOL)canRefresh canLoad:(BOOL)canLoad { if (canRefresh || canLoad) { if (canRefresh) { isCanRefresh = YES; NSDictionary *dic = @{@"pattern":@"%d.png",@"drawingStart":@1,@"drawingEnd":@30,@"loadingStart":@31,@"loadingEnd":@73}; self.drawingImgs = [NSMutableArray array]; self.loadingImgs = [NSMutableArray array]; NSUInteger drawingStart = [dic[@"drawingStart"] intValue]; NSUInteger drawingEnd = [dic[@"drawingEnd"] intValue]; NSUInteger laodingStart = [dic[@"loadingStart"] intValue]; NSUInteger loadingEnd = [dic[@"loadingEnd"] intValue]; for (NSUInteger i = drawingStart; i <= drawingEnd; i++) { NSString *fileName = [NSString stringWithFormat:dic[@"pattern"],i]; [self.drawingImgs addObject:Image([NSString stringWithFormat:@"PaperPullImage/%@",fileName])]; } for (NSUInteger i = laodingStart; i <= loadingEnd; i++) { NSString *fileName = [NSString stringWithFormat:dic[@"pattern"],i]; [self.loadingImgs addObject:Image([NSString stringWithFormat:@"PaperPullImage/%@",fileName])]; } __weak typeof(self) Self = self; [self.tableView addPullToRefreshWithDrawingImgs:self.drawingImgs andLoadingImgs:self.loadingImgs andActionHandler:^{ [Self startRefresh]; }]; } if (canLoad) { isCanLoad = YES; self.tableView.tableFooterView = self.loadFooter; [self.tableView addObserver:self forKeyPath:@"contentOffset" options:NSKeyValueObservingOptionNew context:nil]; }else { isCanLoad = NO; } } } - (void) viewWillDisappear:(BOOL)animated { [super viewWillDisappear:animated]; self.tableView.editing = NO; } - (void)setNextUrl:(NSString *)url { _nextUrl = url; if (isBlankString(url)) { [self noMoreLoad]; }else { self.loadFooter.state = RefreshStateNormal; } } #pragma mark UITableViewDelegate and UITableViewDataSource - (NSInteger)tableView:(UITableView *)tableView numberOfRowsInSection:(NSInteger)section { return 20; } - (CGFloat)tableView:(UITableView *)tableView heightForRowAtIndexPath:(NSIndexPath *)indexPath { return 44.0; } - (CGFloat)tableView:(UITableView *)tableView heightForHeaderInSection:(NSInteger)section { return 0; } - (UITableViewCell *)tableView:(UITableView *)table cellForRowAtIndexPath:(NSIndexPath *)indexPath { static NSString* httpTableViewCellIdentefier = @"TPHttpTableViewController cell"; UITableViewCell* cell = [table dequeueReusableCellWithIdentifier:httpTableViewCellIdentefier]; if (nil == cell) { cell = [[UITableViewCell alloc] initWithStyle:UITableViewCellStyleValue1 reuseIdentifier:httpTableViewCellIdentefier]; cell.backgroundColor = [UIColor whiteColor]; } cell.textLabel.text = [NSString stringWithFormat:@"section:%ld,row=%ld",(long)indexPath.row, (long)indexPath.row]; return cell; } //- (CGFloat)tableView:(UITableView *)tableView heightForFooterInSection:(NSInteger)section { // // This will create a "invisible" footer // return 0.01f; //} // //- (UIView *)tableView:(UITableView *)tableView viewForFooterInSection:(NSInteger)section { // // To "clear" the footer view // return [UIView new]; //} #pragma mark - refresh and load method - (void)manualRefresh { [self.tableView.refreshControl manulHeadRefresh]; } - (void)startRefresh { [self pullRefreshHandler]; if ([self.tableDelegate respondsToSelector:@selector(beginToPullRefresh)]) { [self.tableDelegate beginToPullRefresh]; } } - (void)startLoad { [self pullLoadMoreHander]; if ([self.tableDelegate respondsToSelector:@selector(beginToPullLoad)]) { [self.tableDelegate beginToPullLoad]; } } - (void)pullRefreshHandler{ } - (void)pullLoadMoreHander { } - (void)endRefresh:(void (^)(void))animate completion:(void (^)(void))completion { [self.tableView didFinishPullToRefresh:animate completion:completion]; } - (void)endRefresh { [self.tableView didFinishPullToRefresh:nil completion:nil]; } - (void)noMoreLoad { self.loadFooter.state = RefreshStateNoMore; } - (void)loadSuccess { self.loadFooter.state = RefreshStateSucess; } - (void)loadFailed { self.loadFooter.state = RefreshStateFailed; } - (void)cancelLoadMore { self.loadFooter.state = RefreshStateNormal; } #pragma mark - remote method - (void)startWaitCursor:(int)actionTag { } - (void)stopWaitCursor:(int)actionTag { } - (void)remoteResponsFailed:(int)actionTag withMessage:(NSString *)message resultCode:(NSString *)code{ ShowTextMessage(message); [self endRefresh]; [self loadSuccess]; } #pragma mark - KVO call back - (void)observeValueForKeyPath:(NSString *)keyPath ofObject:(id)object change:(NSDictionary *)change context:(void *)context { if (!self.tableView.userInteractionEnabled || self.tableView.alpha <= 0.01 || self.tableView.hidden) return; if (isCanLoad) { [self loadMethod]; } } - (void)loadMethod { CGFloat y = self.tableView.contentOffset.y + self.tableView.contentInset.top; if (y < 0) { //顶部操作直接返回 return; } if (self.loadFooter.state == RefreshStateRefreshing || self.loadFooter.state == RefreshStateNoMore) { self.loadFooter.hidden = NO; return; } CGPoint offset = self.tableView.contentOffset; CGRect bounds = self.tableView.bounds; CGSize size = self.tableView.contentSize; UIEdgeInsets inset = self.tableView.contentInset; CGFloat currentOffset = offset.y + bounds.size.height - inset.bottom; CGFloat maximumOffset = size.height; CGFloat compareOffset = currentOffset - maximumOffset; if (self.tableView.isDragging ) { if (compareOffset<15 ) { if (!self.tableView.isDecelerating) { self.loadFooter.state = RefreshStateNormal; } }else if (self.loadFooter.state == RefreshStateNormal && (currentOffset - maximumOffset)>20) { self.loadFooter.hidden = NO; if (!self.tableView.isDecelerating) { self.loadFooter.state = RefreshStatePulling; //转为即将刷新 }else { self.loadFooter.state = RefreshStateRefreshing; } } }else { self.loadFooter.hidden = NO; if (self.loadFooter.state == RefreshStatePulling) { self.loadFooter.state = RefreshStateRefreshing; }else { self.loadFooter.state = RefreshStateNormal; } } } - (void)dealloc { if (isCanLoad) { [self.tableView removeObserver:self forKeyPath:@"contentOffset"]; } if (isCanRefresh) { [self.tableView removePullToRefresh]; } } - (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