美文网首页
网络解析(OC_UI)

网络解析(OC_UI)

作者: Miss_差不多 | 来源:发表于2017-08-19 09:01 被阅读7次

    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:(能看懂的大神们自己看吧,如果可以的话随便交流交流,学渣表示很迷茫啊)

    相关文章

      网友评论

          本文标题:网络解析(OC_UI)

          本文链接:https://www.haomeiwen.com/subject/iszzrxtx.html