刷新加载(OC_UI)

2017-08-19  本文已影响7人  Miss_差不多

ViewController.m

#import "ViewController.h"
#import "MJRefresh.h"
#import "EAHTTPClient.h"
#import "Song.h"

@interface ViewController ()
<
UITableViewDataSource,
UITableViewDelegate
>
@property (nonatomic,retain) NSMutableArray *songArray;
@property (nonatomic,retain) UITableView *tableView;
@property (nonatomic,assign) BOOL refresh;
@property (nonatomic,assign) NSInteger page;
@end

@implementation ViewController

- (void)viewDidLoad {
    [super viewDidLoad];
    // Do any additional setup after loading the view, typically from a nib.
    self.songArray = [NSMutableArray array];
    UITableView *tableView = [[UITableView alloc] initWithFrame:self.view.bounds style:UITableViewStylePlain];
    tableView.delegate = self;
    tableView.dataSource = self;
    tableView.backgroundColor = [UIColor whiteColor];
    [self.view addSubview:tableView];
    self.tableView = tableView;
    tableView.mj_header = [MJRefreshNormalHeader headerWithRefreshingBlock:^{
        
        NSLog(@"luck");
        self.refresh = YES;
        self.page = 1;
        [self loadDataWithPage:self.page];
        //结束刷新
        [tableView.mj_header endRefreshing];
    }];
    //自动刷新
    [tableView.mj_header beginRefreshing];
    tableView.mj_footer = [MJRefreshBackNormalFooter footerWithRefreshingBlock:^{
         self.refresh = NO;
        NSLog(@"one");
        self.page++;
        [self loadDataWithPage:self.page];
        [tableView.mj_footer endRefreshing];
    }];
    [tableView.mj_footer beginRefreshing];
    
    
}


- (void)loadDataWithPage:(NSInteger)pageName{
    NSString *URLSreing = @"http://172.18.26.201/get.php";
    NSDictionary * parameters = @{@"page":[NSString stringWithFormat:@"%ld",pageName]};
    [EAHTTPClient GET:URLSreing parameters:parameters success:^(id reponseObject) {
        if (self.refresh) {
            [self.songArray removeAllObjects];
            [self.tableView.mj_header endRefreshing];
        }else{
            [self.tableView.mj_footer endRefreshing];
        }
        
        
        for (NSDictionary *songDic in reponseObject ) {
            Song *song = [[Song alloc] init];
            [song setValuesForKeysWithDictionary:songDic];
            [self.songArray addObject:song];
        }
        [self.tableView reloadData];
        
        } failure:^(NSError *error) {
            
        
    }];


}


-(NSInteger)tableView:(UITableView *)tableView numberOfRowsInSection:(NSInteger)section{
    return self.songArray.count;

}
-(UITableViewCell *)tableView:(UITableView *)tableView cellForRowAtIndexPath:(NSIndexPath *)indexPath{
    Song *song = self.songArray[indexPath.row];
    static NSString *cellId = @"cell";
    UITableViewCell *cell =[tableView dequeueReusableCellWithIdentifier:cellId];
    if (nil == cell) {
        cell = [[UITableViewCell alloc] initWithStyle:UITableViewCellStyleSubtitle reuseIdentifier:cellId];
    }
    cell.textLabel.text = song.name;
    cell.detailTextLabel.text = song.singerName;
    return cell;
}
- (void)didReceiveMemoryWarning {
    [super didReceiveMemoryWarning];
    // Dispose of any resources that can be recreated.
}

@end

Song.h

@property (nonatomic,copy)NSString *name;
@property (nonatomic,copy)NSString *number;
@property (nonatomic,copy)NSString *pic;
@property (nonatomic,copy)NSString *singerName;
@property (nonatomic,copy)NSString *url;

Song.m

- (void)setValue:(id)value forUndefinedKey:(NSString *)key{


}

在这个工程里主要用了第三方框架MJRefresh和EAHTTPClient来实现刷新加载的效果,第一个在github上就可以找到,第二个想要的童鞋可以私信我哦。
这个工程主要就是实现了刷新加载的效果,没有什么深层的知识 大家自行体会吧!!!!

上一篇下一篇

猜你喜欢

热点阅读