UITableview pull up and down load data(UITableview上拉下拉加载数据)
demo:https://github.com/MartinLi841538513/PullDownAndUpLoadDataInTableView
这里引入了一个第三方:MJRefresh,着实好用,稳定。(不多说,代码再次,一看就明白了)
// // RewardRecordsViewController.m // Club // // Created by dongway on 14-8-15. // Copyright (c) 2014年 martin. All rights reserved. // #import "RewardRecordsViewController.h" #import "RewardCell.h" #import "MJRefresh.h" #import "InternetRequest.h" #import "SVProgressHUD.h" @interface RewardRecordsViewController () { __weak IBOutlet UITableView *tableview; NSMutableArray *datas; NSString *identifier; NSInteger page; } @end @implementation RewardRecordsViewController - (id)initWithNibName:(NSString *)nibNameOrNil bundle:(NSBundle *)nibBundleOrNil { self = [super initWithNibName:nibNameOrNil bundle:nibBundleOrNil]; if (self) { // Custom initialization } return self; } -(void)loadView{ [super loadView]; identifier = @"RewardCell"; UINib *nib = [UINib nibWithNibName:@"RewardCell" bundle:nil]; [tableview registerNib:nib forCellReuseIdentifier:identifier]; tableview.delegate = self; tableview.dataSource = self; } - (void)viewDidLoad { [super viewDidLoad]; self.title = @"中奖记录"; datas = [[NSMutableArray alloc] init]; page = 0; [self setupRefresh]; } /** * 集成刷新控件 */
- (void)setupRefresh
{
// 1.下拉刷新(进入刷新状态就会调用self的headerRereshing)
[self.tableview addHeaderWithTarget:self action:@selector(headerRereshing)];
[self.tableview headerBeginRefreshing];
// 2.上拉加载更多(进入刷新状态就会调用self的footerRereshing)
[self.tableview addFooterWithTarget:self action:@selector(footerRereshing)];
// 设置文字(也可以不设置,默认的文字在MJRefreshConst中修改)
self.tableview.headerPullToRefreshText = @"下拉可以刷新了";
self.tableview.headerReleaseToRefreshText = @"松开马上刷新了";
self.tableview.headerRefreshingText = @"正在帮你刷新中";
self.tableview.footerPullToRefreshText = @"上拉可以加载更多数据了";
self.tableview.footerReleaseToRefreshText = @"松开马上加载更多数据了";
self.tableview.footerRefreshingText = @"正在帮你加载中";
}
#pragma mark 开始进入刷新状态 - (void)headerRereshing { page = 1; NSString *pageString = [NSString stringWithFormat:@"%d",page]; NSString *urlString = [NSString stringWithFormat:@"http://earea.stcyclub.com/wap.php/Prize/mylucky?mid=%@&page=%@",@"38",pageString]; dispatch_async(dispatch_get_global_queue(0, 0), ^{ NSDictionary *result = [InternetRequest loadDataWithUrlString:urlString]; dispatch_sync(dispatch_get_main_queue(), ^{ NSNumber *status = (NSNumber *)[result objectForKey:@"status"]; if ([status isEqual:[NSNumber numberWithInt:2]]) { [datas removeAllObjects]; id infos = [result objectForKey:@"info"]; if ([infos isKindOfClass:[NSArray class]]) { [datas addObjectsFromArray:infos]; } [tableview reloadData]; [tableview headerEndRefreshing]; }else{ [SVProgressHUD showErrorWithStatus:@"没有数据"]; [tableview headerEndRefreshing]; page = 0; } }); }); } - (void)footerRereshing { page++; NSString *pageString = [NSString stringWithFormat:@"%d",page]; NSString *urlString = [NSString stringWithFormat:@"http://earea.stcyclub.com/wap.php/Prize/mylucky?mid=%@&page=%@",@"38",pageString]; dispatch_async(dispatch_get_global_queue(0, 0), ^{ NSDictionary *result = [InternetRequest loadDataWithUrlString:urlString]; dispatch_sync(dispatch_get_main_queue(), ^{ NSNumber *status = (NSNumber *)[result objectForKey:@"status"]; if ([status isEqual:[NSNumber numberWithInt:2]]) { id infos = [result objectForKey:@"info"]; if ([infos isKindOfClass:[NSArray class]]) { [datas addObjectsFromArray:infos]; [tableview reloadData]; }else{ [SVProgressHUD showErrorWithStatus:@"没有更多数据了"]; } [tableview footerEndRefreshing]; }else{ [SVProgressHUD showErrorWithStatus:@"没有更多数据了"]; [tableview footerEndRefreshing]; page--; } }); }); } - (void)didReceiveMemoryWarning { [super didReceiveMemoryWarning]; // Dispose of any resources that can be recreated. } -(void)updateViewConstraints{ [super updateViewConstraints]; } #pragma mark - Table view data source - (NSInteger)numberOfSectionsInTableView:(UITableView *)tableView { // Return the number of sections. return datas.count; } - (NSInteger)tableView:(UITableView *)tableView numberOfRowsInSection:(NSInteger)section { // Return the number of rows in the section. return 1; } - (UITableViewCell *)tableView:(UITableView *)tableView cellForRowAtIndexPath:(NSIndexPath *)indexPath { NSUInteger row = indexPath.section; RewardCell *cell = [tableView dequeueReusableCellWithIdentifier:identifier forIndexPath:indexPath]; NSDictionary *data = [datas objectAtIndex:row]; // Configure the cell... cell.rewardValue.text = [data objectForKey:@"cash"]; cell.time.text = [data objectForKey:@"regtime"]; return cell; } -(CGFloat)tableView:(UITableView *)tableView heightForRowAtIndexPath:(NSIndexPath *)indexPath{ return 54; } @end