IOS刷新

在刷新功能中,我們借用MJRefresh第三方來實現(xiàn)刷新功能

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];
}
// 獲取網(wǎng)絡數(shù)據(jù)
-(void)getURLDatas{
    dispatch_async(dispatch_get_main_queue(), ^{
        // 模擬網(wǎng)絡延時狀態(tài)
          [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

?著作權(quán)歸作者所有,轉(zhuǎn)載或內(nèi)容合作請聯(lián)系作者
平臺聲明:文章內(nèi)容(如有圖片或視頻亦包括在內(nèi))由作者上傳并發(fā)布,文章內(nèi)容僅代表作者本人觀點,簡書系信息發(fā)布平臺,僅提供信息存儲服務。

推薦閱讀更多精彩內(nèi)容