下拉刷新和上拉加载更多(第三方框架MJRefresh)
2015-07-06 20:01
387 查看
#import "RootViewController.h" #import "MJRefresh.h" @interface RootViewController ()<UITableViewDataSource,UITableViewDelegate,MJRefreshBaseViewDelegate> { UITableView *_tableView ; NSMutableArray *datas; MJRefreshHeaderView *headerView; MJRefreshFooterView *footerView; } @end @implementation RootViewController - (void)dealloc { [headerView free]; [footerView free]; } - (void)viewDidLoad { [super viewDidLoad]; //初始化tableView [self initializeTableView]; // 初始化数据 [self initializeData]; } - (void)initializeTableView { _tableView = [[UITableView alloc] initWithFrame:[[UIScreen mainScreen] bounds] style:UITableViewStylePlain]; _tableView.dataSource = self; [self.view addSubview:_tableView]; // 初始化刷新控件 [self initializeRefreshView]; } - (void)initializeRefreshView { // 下拉刷新 headerView = [MJRefreshHeaderView header]; headerView.scrollView = _tableView; headerView.delegate = self; // 上拉加载更多 footerView = [MJRefreshFooterView footer]; footerView.delegate = self; footerView.scrollView = _tableView; } /** * 刷新控件进入开始刷新状态的时候调用 */ - (void)refreshViewBeginRefreshing:(MJRefreshBaseView *)refreshView { if ([refreshView isKindOfClass:[MJRefreshHeaderView class]]) { //下拉刷新 [self loadNewData]; }else{ [self loadMoreData]; } } /* * 刷新数据 */ - (void)loadNewData { static int count = 1; NSMutableArray *array = [NSMutableArray array]; //每次刷新五条数据 for (int i = 0; i < 5; i++) { NSString *str = [NSString stringWithFormat:@"第%d次刷新",count]; [array addObject:str]; } // 把新数据加到datas前面 NSMutableArray *newArray = [[NSMutableArray alloc] initWithArray:array]; [newArray addObjectsFromArray:datas]; datas = newArray; [_tableView reloadData]; [headerView endRefreshing]; count++; } /* * 加载更多数据 */ - (void)loadMoreData { static int count = 1; NSMutableArray *array = [NSMutableArray array]; for (int j = 0; j < 5; j++) { NSString *str = [NSString stringWithFormat:@"第%d次加载更多",count]; [array addObject:str]; } [datas addObjectsFromArray:array]; [_tableView reloadData]; [footerView endRefreshing]; count++; } - (void)initializeData { datas = [NSMutableArray arrayWithObjects:@"A",@"B", @"C", @"D", @"E", @"F", @"G", @"H", @"I", @"J", @"K", @"L", @"M", @"N", @"O", @"P", @"Q", @"R", @"S", @"T", @"U", @"V", @"W", @"X", @"Y", @"Z", nil]; } #pragma mark -配置UITableViewDataSource数据 - (NSInteger)tableView:(UITableView *)tableView numberOfRowsInSection:(NSInteger)section { return [datas count]; } - (UITableViewCell *)tableView:(UITableView *)tableView cellForRowAtIndexPath:(NSIndexPath *)indexPath { static NSString *identify = @"cell"; UITableViewCell *cell = [_tableView dequeueReusableCellWithIdentifier:identify]; if (!cell) { cell = [[UITableViewCell alloc] initWithStyle:UITableViewCellStyleDefault reuseIdentifier:identify]; } cell.textLabel.text = datas[indexPath.row]; return cell; } @end
相关文章推荐
- Cobbler Web界面操作(四)
- 申请免费ssl证书 StartSSL
- 数组中两个元素的最小距离
- Unity 泛型T的用法
- python描述符
- 图的最短路径算法Dijkstra算法
- 汉诺塔-递归实现
- 里约,我们来了
- 使用队列实时显示波形图实现
- 【Web缓存机制系列】
- Oracle pl/sql中的group by子句不能使用别名,应该这样写。
- Java中只有按值传递,没有按引用传递!
- linux网络设备驱动DM9000驱动分析(4)
- requirejs 基础
- 欢迎使用CSDN-markdown编辑器
- Asp.Net Ajax4.5 未知的服務器標記 問題
- 简单区分VMware的三种网络连接模式(bridged、NAT、host-only)
- 动态树 解题报告
- Wildcard Matching 字符串含?,*匹配
- linux 文件管理