網路解析(OC_UI)

weixin_33860722發表於2017-08-19

ViewController.m

#import "ViewController.h"
#import "Song.h"

@interface ViewController ()
<
UITableViewDataSource,
UITableViewDelegate
>

@property(nonatomic,retain)NSMutableArray *songArray;
@property (nonatomic,retain)UITableView *tableView;
@property (nonatomic,assign)NSInteger pageNumber;
@property (nonatomic,assign)BOOL isRefresh;
@end

@implementation ViewController

- (void)viewDidLoad {
    [super viewDidLoad];
    self.pageNumber = 1;
    
    self.songArray = [[NSMutableArray alloc] init];
    
    
    UITableView *tableView = [[UITableView alloc] initWithFrame:self.view.bounds style:UITableViewStylePlain];
    tableView.dataSource = self;
    tableView.delegate = self;
    [self.view addSubview:tableView];
    self.tableView = tableView;
    NSString *URLString = @"http://172.18.26.196/get.php?page=1";
    [self getDataWithURLString:URLString];

   }


-(void)scrollViewDidEndDragging:(UIScrollView *)scrollView willDecelerate:(BOOL)decelerate{
    
    CGFloat offsety = scrollView.contentOffset.y;
    CGFloat contentHeight = scrollView.contentSize.height;
    if (contentHeight <= scrollView.frame.size.height) {
        contentHeight = scrollView.frame.size.height;
    }
    if (offsety >= contentHeight - scrollView.frame.size.height + 50) {
        self.isRefresh = NO;
        self.pageNumber++;
        NSString *URLString = [NSString stringWithFormat:@"http://172.18.26.196/get.php?page=%ld",self.pageNumber];
        [self getDataWithURLString:URLString];
    }else if (offsety <= -80.f){
        self.isRefresh = YES;
        self.pageNumber = 1;
        NSString *URLString = [NSString stringWithFormat:@"http://172.18.26.196/get.php?page=%ld",self.pageNumber];
        [self getDataWithURLString:URLString];
        
        
    }
    NSLog(@"%lf",offsety) ;


}

- (void)getDataWithURLString:(NSString *)URLString{
    NSURL *url = [NSURL URLWithString:URLString];
    NSMutableURLRequest *request = [NSMutableURLRequest requestWithURL:url];
    NSURLSession *session = [NSURLSession sharedSession];
    NSURLSessionDataTask *dataTask = [session dataTaskWithRequest:request completionHandler:^(NSData * _Nullable data, NSURLResponse * _Nullable response, NSError * _Nullable error) {
        
        //非同步             主執行緒
        dispatch_async(dispatch_get_main_queue(), ^{
            
            
            if (self.isRefresh == YES) {
                [self.songArray removeAllObjects];
            }
            

            if (!error) {
                id result = [NSJSONSerialization JSONObjectWithData:data options:NSJSONReadingMutableContainers error:nil];
                NSLog(@"result:%@",result);
                
                for (NSDictionary *songDic in result) {
                    Song *song = [[Song alloc] init];
                    [song setValuesForKeysWithDictionary:songDic];
                    [self.songArray addObject:song];
                }
                //重新整理tableView
                [self.tableView reloadData];
                
            }else{
                
                NSLog(@"error :%@",error);
            }
        });
    }];
    [dataTask resume];

}

-(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 *songId = @"song";
    UITableViewCell *cell = [tableView dequeueReusableCellWithIdentifier:songId];
    if (nil == cell) {
        cell = [[UITableViewCell alloc] initWithStyle:UITableViewCellStyleSubtitle reuseIdentifier:songId];
    }
    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{


}

這是ios中的網路解析,是非同步的。
PS:(能看懂的大神們自己看吧,如果可以的話隨便交流交流,學渣表示很迷茫啊)

相關文章