解析AF

解析到表格

- (void)viewDidLoad {
    self.datasource = [NSMutableArray array];
    AFHTTPSessionManager *manager = [AFHTTPSessionManager manager];
    manager.responseSerializer.acceptableContentTypes = [NSSet setWithObject:@"application/json"];
    [manager GET:@"http://live.ximalaya.com/live-web/v4/homepage?device=iPhone" parameters:nil progress:nil success:^(NSURLSessionDataTask * _Nonnull task, id  _Nullable responseObject) {
        
        NSLog(@"%@",responseObject);
        for (NSDictionary *dic in responseObject[@"data"][@"topRadios"]) {
            Modeltwo *model = [Modeltwo new];
            [model setValuesForKeysWithDictionary:dic];
            [self.datasource addObject:model];
        }
        

        dispatch_async(dispatch_get_main_queue(), ^{
            [self.tableview reloadData];
        });
        
        
    } failure:^(NSURLSessionDataTask * _Nullable task, NSError * _Nonnull error) {
        NSLog(@"%@",error);
    }];
    
    
    
    
    
    
    
    
    [super viewDidLoad];
    
    _tableview = [[UITableView alloc]initWithFrame:CGRectMake(0,0, self.view.frame.size.width, self.view.frame.size.height) style:UITableViewStylePlain];
    _tableview.delegate = self;
    _tableview.dataSource = self;
    _tableview.rowHeight = 100;
    [self.view addSubview:self.tableview];
    
    [_tableview registerClass:[TableViewCell class] forCellReuseIdentifier:@"cell"];
    
}
-(NSInteger)tableView:(UITableView *)tableView numberOfRowsInSection:(NSInteger)section{
    return self.datasource.count;
}
-(UITableViewCell *)tableView:(UITableView *)tableView cellForRowAtIndexPath:(NSIndexPath *)indexPath{
    TableViewCell *cell = [tableView dequeueReusableCellWithIdentifier:@"cell"];
    if (cell == nil) {
        cell = [[TableViewCell alloc]initWithStyle:UITableViewCellStyleDefault reuseIdentifier:@"cell"];
        
    }
    if (self.datasource.count > 0) {
        Modeltwo *model = self.datasource[indexPath.row];
        NSURL *url = [NSURL URLWithString:model.imageUrl];
        NSData *data = [NSData dataWithContentsOfURL:url];
        cell.img.image = [UIImage imageWithData:data];
        cell.lab1.text = model.name;
        cell.lab2.text = model.localRadios;
//        cell.lab2.text  = model.viewCount;
        
    }
    
    
    return cell;
}

解析到网格

- (void)viewDidLoad {
    [super viewDidLoad];
    self.datasource = [NSMutableArray array];
    
    AFHTTPSessionManager *manager = [AFHTTPSessionManager manager];
    manager.responseSerializer.acceptableContentTypes = [NSSet setWithObject:@"application/json"];
    [manager GET:@"http://123.126.40.109:7003/asmr/videos/A1100101.shtml" parameters:nil progress:nil success:^(NSURLSessionDataTask * _Nonnull task, id  _Nullable responseObject) {
        NSLog(@"%@",responseObject);
        for (NSDictionary * dic in responseObject[@"result"]) {
            Model *model = [Model new];
            [model setValuesForKeysWithDictionary:dic];
            [self.datasource addObject:model];
        }
        dispatch_async(dispatch_get_main_queue(), ^{
            [self.clv reloadData];
        });
        
    } failure:^(NSURLSessionDataTask * _Nullable task, NSError * _Nonnull error) {
        NSLog(@"%@",error);
        
    }];

    
    UICollectionViewFlowLayout *lou = [[UICollectionViewFlowLayout alloc]init];
    lou.itemSize = CGSizeMake(self.view.frame.size.width/2, 220);
    lou.scrollDirection = UICollectionViewScrollDirectionHorizontal;
    lou.minimumLineSpacing = 0;
    lou.minimumInteritemSpacing = 0;
    lou.sectionInset = UIEdgeInsetsMake(0,0,0,0);
    
 
    _clv = [[UICollectionView alloc]initWithFrame:CGRectMake(0, 0, self.view.frame.size.width, self.view.frame.size.height) collectionViewLayout:lou];
    _clv.delegate = self;
    _clv.dataSource = self;
    _clv.backgroundColor = [UIColor whiteColor];
    
    
    [self.view addSubview:self.clv];
    
//    [_clv registerClass:[CollectionViewCell class] forCellWithReuseIdentifier:reuseID];
    [_clv registerNib:[UINib nibWithNibName:@"CollectionViewCell" bundle:nil] forCellWithReuseIdentifier:reuseID];
}
// 设置分区

// 设置多少个item
- (NSInteger)collectionView:(UICollectionView *)collectionView numberOfItemsInSection:(NSInteger)section
{
    return self.datasource.count;
}

// 设置 cell
- (UICollectionViewCell *)collectionView:(UICollectionView *)collectionView cellForItemAtIndexPath:(NSIndexPath *)indexPath
{
    
    // 根据可重用标识符查找cell
    CollectionViewCell *cell = [collectionView dequeueReusableCellWithReuseIdentifier:reuseID forIndexPath:indexPath];
//    cell.backgroundColor = [UIColor redColor];
    // 设置cell内容
    if (self.datasource.count>0) {
        Model *model = _datasource[indexPath.item];
//        cell.img.image = [UIImage imageNamed:model.imageUrl];
        NSURL *url = [NSURL URLWithString:model.imageUrl];
        NSData *data = [NSData dataWithContentsOfURL:url];
        cell.img.image = [UIImage imageWithData:data];
        cell.lab1.text = model.fileDescribe;
        cell.lab2.text = model.name;
    }
    // 返回 cell
    return cell;
    
}



@end

猜你喜欢

转载自blog.csdn.net/chuck_phonics/article/details/84670771
AF