如何将我的url json数据显示到集合视图

时间:2015-11-02 06:31:39

标签: ios objective-c json

我从一个带有授权标头的网址获取数据,并转换为json

在我的json我只有pdf文件。但我不想显示pdf文件。我想显示文件名(例如:document1.pdfdocument2.pdf)。我还有3个部分 - 每个包含10个文档。所以我需要显示文档的名称。

我看到一些只使用图像而非文档文件的教程。那么如何将我的文档名称单独显示到集合视图。

还有一个问题。我需要解码以显示我的数据或不想这样做。帮助我。谢谢你!

1 个答案:

答案 0 :(得分:2)

    NSMutableArry *arrayPDFName  = [[NSMutableArray alloc]init];
    NSDictionary *jsonResults = [NSJSONSerialization JSONObjectWithData:returnData options:NSJSONReadingMutableContainers error:nil];
    NSDictionary *dictOriginal = [jsonResultsvalueForKey:@"original"];
    NSArray *arrayFiles = [[dictOriginal valueForKey:@"files"] copy];
    NSLog(@"The arrayFiles are - %@",arrayFiles);
    for(int i=0;i<[arrayFiles count];i++)
    {
        NSString *strCreatedTime = [NSString stringWithFormat:@"%@",[[arrayFiles objectAtIndex:i] valueForKey:@"created_time"]];
        NSString *strLastModifiedTime = [NSString stringWithFormat:@"%@",[[arrayFiles objectAtIndex:i] valueForKey:@"last_modified_time"]];
        NSString *strID = [NSString stringWithFormat:@"%@",[[arrayFiles objectAtIndex:i] valueForKey:@"id"]];
        NSString *strName = [NSString stringWithFormat:@"%@",[[arrayFiles objectAtIndex:i] valueForKey:@"name"]];

        NSLog(@"The created_time is - %@",strCreatedTime);
        NSLog(@"The last_modified_time is - %@",strLastModifiedTime);
        NSLog(@"The is is - %@",strID);
        NSLog(@"The name is - %@",strName);

        [arrayPDFName addObject:strName];
    }

}
在collectionview中,您需要使用上面的arrayPDFName(在集合视图委托方法中)

在CollectionView中显示数据

- (void)viewDidLoad 
{
  [super viewDidLoad];
  [self getResponse];
  UICollectionViewFlowLayout *flowLayout = [[UICollectionViewFlowLayout alloc] init];
  flowLayout.scrollDirection =  UICollectionViewScrollDirectionVertical;
  //Register the custom cell for collection view
  UINib *cellNib = [UINib nibWithNibName:@"CustomCell" bundle:nil];
  [collectionViewHorizontalVertical registerNib:cellNib forCellWithReuseIdentifier:@"cvCell"];
}

//Collection View Delegates method
-(NSInteger)numberOfSectionsInCollectionView:(UICollectionView *)collectionView
{
  return  1;
}

-(NSInteger)collectionView:(UICollectionView *)collectionView numberOfItemsInSection:(NSInteger)section
{
  return arrayPDFName.count;
}

-(UICollectionViewCell *)collectionView:(UICollectionView *)collectionView cellForItemAtIndexPath:(NSIndexPath *)indexPath
{
   static NSString *cellIdentifier = @"cvCell";
CustomCell *cell = (CustomCell *)[collectionView dequeueReusableCellWithReuseIdentifier:cellIdentifier forIndexPath:indexPath];
          //data for text
  cell.textField.text = [arrayPDFName objectAtIndex:indexPath.row];

           //OR Image

  cell.imgViewCollection.image = [UIImage imageNamed:[arrayPDFName objectAtIndex:indexPath.row]];

  return cell;
}

// If you want to set the cell height
- (CGSize)collectionView:(UICollectionView *)collectionView layout:(UICollectionViewLayout*)collectionViewLayout sizeForItemAtIndexPath:(NSIndexPath *)indexPath 
{
  return CGSizeMake(200, 200); //Please give your required size
}