Ios UICollectionView显示为空
Hi在我的应用程序中,尝试在我的Ios UICollectionView显示为空,ios,objective-c,uicollectionview,Ios,Objective C,Uicollectionview,Hi在我的应用程序中,尝试在我的UICollectionView中显示Flickr图像,但它显示了一个空的视图控件 我的代码用于显示Flickr图像 { NSMutableArray *photoURLs; NSMutableArray *photoSetNames; NSMutableArray *photoids; } viewDidLoadcode. - (void)viewDidLoad { [super viewDidLoad]; photoURLs = [
UICollectionView
中显示Flickr
图像,但它显示了一个空的视图控件
我的代码用于显示Flickr
图像
{
NSMutableArray *photoURLs;
NSMutableArray *photoSetNames;
NSMutableArray *photoids;
}
viewDidLoad
code.- (void)viewDidLoad
{
[super viewDidLoad];
photoURLs = [[NSMutableArray alloc] init];
photoSetNames = [[NSMutableArray alloc] init];
photoids = [[NSMutableArray alloc] init];
[self loadFlickrPhotos];
[self.collectionview reloadData];
}
- (void)loadFlickrPhotos {
NSString *urlString = [NSString stringWithFormat:@"https://www.flickr.com/services/rest/?method=flickr.photosets.getPhotos&format=json&api_key=a6a0c7d5efccffc285b0fe5ee1d938e3&photoset_id=72157644758906604&per_page=10&nojsoncallback=1",nil];
NSLog(@"the url string==%@",urlString);
NSURL *url = [NSURL URLWithString:urlString];
NSString *jsonString = [NSString stringWithContentsOfURL:url encoding:NSUTF8StringEncoding error:nil];
NSLog(@"the str==%@",jsonString);
NSDictionary *results = [NSJSONSerialization JSONObjectWithData:[jsonString dataUsingEncoding:NSUTF8StringEncoding] options:0 error:nil];
NSArray *photosets = [[results objectForKey:@"photosets"] objectForKey:@"photoset"];
for (NSDictionary *photoset in photosets) {
NSString *title = [[photoset objectForKey:@"title"] objectForKey:@"_content"];
NSLog(@"title==%@",title);
[photoSetNames addObject:(title.length > 0 ? title : @"Untitled")];
NSString *primary = [photoset objectForKey:@"primary"];
NSString *server = [photoset objectForKey:@"server"];
NSString *secret = [photoset objectForKey:@"secret"];
NSString *farm = [photoset objectForKey:@"farm"];
NSString *urlstr=[NSString stringWithFormat:@"http://farm%@.staticflickr.com/%@/%@_%@.jpg",farm,server,primary,secret];
NSLog(@"your photo id==%@",urlstr);
[photoids addObject:urlstr];
}
}
- (NSInteger)collectionView:(UICollectionView *)collectionView numberOfItemsInSection:(NSInteger)section {
return [photoids count];
}
- (NSInteger)numberOfSectionsInCollectionView: (UICollectionView *)collectionView {
return 1;
}
- (UICollectionViewCell *)collectionView:(UICollectionView *)collectionView cellForItemAtIndexPath:(NSIndexPath *)indexPath {
static NSString *cellIdentifier =@"Cell";
imggpolitical *cell=(imggpolitical *) [collectionView dequeueReusableCellWithReuseIdentifier:cellIdentifier forIndexPath: indexPath];
cell.imageview.image=[UIImage imageWithData:[NSData dataWithContentsOfURL:[NSURL URLWithString:[NSString stringWithFormat:@"%@",[photoids objectAtIndex:indexPath.row]]]]];
return cell;
}
我已使用上述内容在UICollectionView
中显示Flickr
图像,但它是一个空视图,请告诉我如何解决此问题
谢谢。尝试
[self.collectionview reloadData]
在-(void)loadFlickrPhotos方法的末尾,在UICollectionView上调用reloadData。您需要告诉UICollectionView刷新其数据。我看不到您正在分配委托或数据源。或者为此,创建了UICollectionView。您是否制作了ViewController UICollectionViewDelegate和UICollectionViewDatasource?@remus s我已经连接了代理和数据源,并且在我的ViewController中,我也已经连接了,这是您的原型单元(故事板上)单元标识符设置为“单元”?@ShivamMishra我已经在我的故事板中设置了单元格作为标识符,你得到的是黑屏吗??尝试在cellforRow方法中为单元格赋予颜色,检查单元格内容视图中是否未布置单元格或未显示图像。我在loadflickr方法的末尾调用了[self.collectionview reloadData]但在自定义UICollectionViewCell中仍然不起作用您是否设置了imageview出口?您到底在哪里调用reloddata方法?在ViewDidLoad中的LoadFlickphotos之后调用它我在ViewDidLoad中调用了loadflickr方法,但它不起作用