1 #import "RootViewController.h" 2 #import "MJRefresh.h" 3 @interface RootViewController ()4 { 5 UITableView *_tableView ; 6 NSMutableArray *datas; 7 MJRefreshHeaderView *headerView; 8 MJRefreshFooterView *footerView; 9 } 10 @end 11 12 @implementation RootViewController 13 14 - (void)dealloc 15 { 16 [headerView free]; 17 [footerView free]; 18 } 19 20 - (void)viewDidLoad { 21 [super viewDidLoad]; 22 //初始化tableView 23 [self initializeTableView]; 24 // 初始化数据 25 [self initializeData]; 26 } 27 28 - (void)initializeTableView 29 { 30 _tableView = [[UITableView alloc] initWithFrame:[[UIScreen mainScreen] bounds] style:UITableViewStylePlain]; 31 _tableView.dataSource = self; 32 [self.view addSubview:_tableView]; 33 // 初始化刷新控件 34 [self initializeRefreshView]; 35 } 36 37 38 - (void)initializeRefreshView 39 { 40 // 下拉刷新 41 headerView = [MJRefreshHeaderView header]; 42 headerView.scrollView = _tableView; 43 headerView.delegate = self; 44 45 // 上拉加载更多 46 footerView = [MJRefreshFooterView footer]; 47 footerView.delegate = self; 48 footerView.scrollView = _tableView; 49 } 50 51 /** 52 * 刷新控件进入开始刷新状态的时候调用 53 */ 54 - (void)refreshViewBeginRefreshing:(MJRefreshBaseView *)refreshView 55 { 56 if ([refreshView isKindOfClass:[MJRefreshHeaderView class]]) { 57 //下拉刷新 58 [self loadNewData]; 59 }else{ 60 [self loadMoreData]; 61 } 62 } 63 /* 64 * 刷新数据 65 */ 66 - (void)loadNewData 67 { 68 static int count = 1; 69 NSMutableArray *array = [NSMutableArray array]; 70 //每次刷新五条数据 71 for (int i = 0; i < 5; i++) { 72 NSString *str = [NSString stringWithFormat:@"第%d次刷新",count]; 73 [array addObject:str]; 74 } 75 // 把新数据加到datas前面 76 NSMutableArray *newArray = [[NSMutableArray alloc] initWithArray:array]; 77 [newArray addObjectsFromArray:datas]; 78 datas = newArray; 79 [_tableView reloadData]; 80 [headerView endRefreshing]; 81 count++; 82 } 83 84 /* 85 * 加载更多数据 86 */ 87 - (void)loadMoreData 88 { 89 static int count = 1; 90 NSMutableArray *array = [NSMutableArray array]; 91 for (int j = 0; j < 5; j++) { 92 NSString *str = [NSString stringWithFormat:@"第%d次加载更多",count]; 93 [array addObject:str]; 94 } 95 [datas addObjectsFromArray:array]; 96 [_tableView reloadData]; 97 [footerView endRefreshing]; 98 count++; 99 }100 101 - (void)initializeData102 {103 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];104 }105 106 #pragma mark -配置UITableViewDataSource数据107 - (NSInteger)tableView:(UITableView *)tableView numberOfRowsInSection:(NSInteger)section108 {109 return [datas count];110 }111 112 - (UITableViewCell *)tableView:(UITableView *)tableView cellForRowAtIndexPath:(NSIndexPath *)indexPath113 {114 static NSString *identify = @"cell";115 UITableViewCell *cell = [_tableView dequeueReusableCellWithIdentifier:identify];116 if (!cell) {117 cell = [[UITableViewCell alloc] initWithStyle:UITableViewCellStyleDefault reuseIdentifier:identify];118 119 }120 cell.textLabel.text = datas[indexPath.row];121 return cell;122 }123 124 @end