如何将我的url json数据显示到集合视图 [英] How to show my url json data to collection view
问题描述
我从具有授权标头的一个URL中获取数据,然后转换为json
.
I have get data from one url with authorized header and i convert to json
.
在我的json
中,我只有pdf文件.但我不想显示pdf文件.我想显示文件名(例如:document1.pdf
,document2.pdf
).我还有3个部分-每个部分包含10个文档.所以我需要显示文件名称.
In my json
i am having only pdf file. But i don't want to show pdf file. I want to show the file name ( eg : document1.pdf
, document2.pdf
).and also i am having 3 section - each contain 10 documents. So i need to show name of the document.
我看到了一些仅使用图像而不使用文档文件的教程.那么如何仅在集合视图中显示我的文档名称.
I saw some tutorials which using only image not document file. So how to show my document name alone to collection view.
还有一个问题.我需要解码以显示我的数据还是不想这样做?帮帮我.谢谢!!
One more question. does i need to decode to show my data or don't want to do that. Help me out.Thanks in advance !
推荐答案
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(在collection view委托方法中)
in collectionview you need use the above arrayPDFName(in collection view delegates methods)
在CollectionView中显示数据
Showing data in 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
}
这篇关于如何将我的url json数据显示到集合视图的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!