【发布时间】:2014-05-22 06:46:58
【问题描述】:
您好,在我的应用程序中尝试在我的 UICollectionView 中显示 Flickr 图像,但它显示一个空的视图控件
我的代码用于显示Flickr 图像。
{
NSMutableArray *photoURLs;
NSMutableArray *photoSetNames;
NSMutableArray *photoids;
}
viewDidLoad 代码。
- (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 图像,但它以空视图出现,请告诉我如何解决此问题。
谢谢。
【问题讨论】:
-
你的 ViewController UICollectionViewDelegate 和 UICollectionViewDatasource 做好了吗?
-
@remus s 我已经连接了委托和数据源,在我的视图控制器中我也这样做了
-
您的原型单元格(在情节提要上)的单元格标识符是否设置为“单元格”?
-
@ShivamMishra 我已经在我的故事板中将单元格设置为标识符
-
你是不是黑屏了??尝试在 cellforRow 方法中为单元格添加颜色,检查单元格是否没有布局或图像没有显示在单元格内容视图上。
标签: ios objective-c uicollectionview