上下拉 刷新菜单

加入第三方 MJRefresh

导入头文件

6348937-bff674ded84dc1f1.png

定义全局变量 以及设置代理

上下拉 刷新菜单_第1张图片
6348937-ad2218a58e2efeb3.png
@interface ViewController (){
UITableView *table;
NSMutableArray *arr;
MJRefreshFooterView *foot;
MJRefreshHeaderView *header;
}

- (void)viewDidLoad {
[super viewDidLoad];
// Do any additional setup after loading the view, typically from a nib.
//设置表格
table = [[UITableView alloc]initWithFrame:CGRectMake(0, 20, self.view.frame.size.width, self.view.frame.size.height) style:UITableViewStylePlain];
table.dataSource = self;
table.delegate = self;
[self.view addSubview: table];
arr = [NSMutableArray arrayWithObjects:@"123",@"345",@"123",@"345",@"123",@"345", nil];
foot = [MJRefreshFooterView footer];
foot.scrollView = table;
foot.delegate = self;
header = [MJRefreshHeaderView header];
header.scrollView = table;
header.delegate = self;
//开始刷新
[header beginRefreshing];
}

点击MJRefreshBaseViewDelegate 出来下一步

- (void)refreshViewBeginRefreshing:(MJRefreshBaseView *)refreshView{
if ([refreshView isKindOfClass:[MJRefreshHeaderView class]]) {
//2秒以后调用
dispatch_after(dispatch_time(DISPATCH_TIME_NOW, (int64_t)(2.0 * NSEC_PER_SEC)), dispatch_get_main_queue(), ^{
// 10    1
[arr addObject:@"new123"];
//刷新表格
[table reloadData];
//停止刷新
[foot endRefreshing];
});
}else{
dispatch_after(dispatch_time(DISPATCH_TIME_NOW, (int64_t)(2.0 * NSEC_PER_SEC)), dispatch_get_main_queue(), ^{
[arr removeObjectAtIndex:0];
//刷新表格
[table reloadData];
//停止刷新
[foot endRefreshing];
});
}
}
-(NSInteger)tableView:(UITableView *)tableView numberOfRowsInSection:(NSInteger)section{
return arr.count;
}
-(UITableViewCell *)tableView:(UITableView *)tableView cellForRowAtIndexPath:(NSIndexPath *)indexPath{
static  NSString *cellId = @"cellid";
UITableViewCell *cell = [tableView dequeueReusableCellWithIdentifier:cellId];
if (!cell) {
cell = [[UITableViewCell alloc]initWithStyle:UITableViewCellStyleDefault reuseIdentifier:cellId];
}
cell.textLabel.text = arr[indexPath.row];
return cell;
}
-(CGFloat)tableView:(UITableView *)tableView heightForFooterInSection:(NSInteger)section{
return 1;
}

你可能感兴趣的:(上下拉 刷新菜单)