在刷新功能中,我们借用MJRefresh第三方来实现刷新功能
1 将第三方 导入工程中
2 viewController.m文件中
#import "ViewController.h"
#import "MJRefresh.h"
@interface ViewController ()<UITableViewDataSource>
{
NSMutableArray * _tableData;
}
@property(nonatomic,strong)UITableView *table;
@property(nonatomic,strong)MJRefreshHeaderView * MJheadView;
@end
@implementation ViewController
- (void)viewDidLoad {
[super viewDidLoad];
_tableData = [@[@"1",@"2",@"3",@"4"]mutableCopy];
self.table = [[UITableView alloc] initWithFrame:self.view.frame style:UITableViewStylePlain];
self.table.dataSource = self;
[self.view addSubview:self.table];
self.MJheadView = [[MJRefreshHeaderView alloc] initWithScrollView:self.table];
__weak ViewController *weakSelf = self;
weakSelf.MJheadView.beginRefreshingBlock = ^(MJRefreshBaseView *refreshView) {
[self getURLDatas];
};
[self.MJheadView beginRefreshing];
}
// 获取网络数据
-(void)getURLDatas{
dispatch_async(dispatch_get_main_queue(), ^{
// 模拟网络延时状态
[NSThread sleepForTimeInterval:3.0];
});
[_tableData addObject:@"jyh"];
dispatch_async(dispatch_get_main_queue(), ^{
[self.table reloadData];
if (self.MJheadView.refreshing) {
[self.MJheadView endRefreshing];
}
});
}
-(NSInteger)tableView:(UITableView *)tableView numberOfRowsInSection:(NSInteger)section{
return _tableData.count;
}
- (UITableViewCell *)tableView:(UITableView *)tableView cellForRowAtIndexPath:(NSIndexPath *)indexPath{
static NSString * iderfire = @"cell";
UITableViewCell *cell = [tableView dequeueReusableCellWithIdentifier:iderfire];
if (!cell) {
cell = [[UITableViewCell alloc] initWithStyle:UITableViewCellStyleDefault reuseIdentifier:iderfire];
}
cell.textLabel.text = _tableData[indexPath.row];
return cell;
}
@end