【问题标题】:UICollectionView Displays emptyUICollectionView 显示为空
【发布时间】: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


【解决方案1】:

在 -(void)loadFlickrPhotos 方法的最后,在你的 UICollectionView 上调用 reloadData。你需要告诉 UICollectionView 刷新它的数据。

【讨论】:

  • 我在 loadflickr 方法结束时调用了 [self.collectionview reloadData] 但仍然无法正常工作
  • 在您的自定义 UICollectionViewCell 中是否设置了 imageview 出口?
【解决方案2】:

试试[self.collectionview reloadData]

【讨论】:

  • 你到底在哪里调用 reloddata 方法?在 viewDidLoad 中 loadFlickerPhotos 之后调用它
  • 我在我的 loadflickr 方法之后调用了它,因为它没有工作
【解决方案3】:

我没有看到您正在分配委托或数据源。或者就此而言,创建您的 UICollectionView。

【讨论】:

    猜你喜欢
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 2014-12-27
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    相关资源
    最近更新 更多