Vix Hunk Vix Hunk - 3 months ago 13
iOS Question

Facebook loading albums in uitableview but not opening in next collection view

i am trying to access facebook albums and i implemented it in my app but when i ran it. it shows all the albums but not showing pictures in those albums when i select a album. here is my code
NOTE: there is a xib for albumsviewcontroller but no xib for FacebookPhotoViewController so do i need to create one?

here is cellforraw method from albumsviewcontroller

-(UITableViewCell *)tableView:(UITableView *)tableView cellForRowAtIndexPath:(NSIndexPath *)indexPath
{
RTFacebookAlbumCell* cell = (RTFacebookAlbumCell*)[tableView dequeueReusableCellWithIdentifier:@"cell"];
if (!cell) {
cell = [[NSBundle mainBundle] loadNibNamed:@"RTFacebookAlbumCell" owner:self options:nil][0];
}
cell.albumName.text = _datasource[indexPath.row][@"name"];
for (NSDictionary* dict in _albumCoverArray) {
if ([[dict objectForKey:@"index"] intValue] == indexPath.row) {
dispatch_async(dispatch_get_global_queue(DISPATCH_QUEUE_PRIORITY_BACKGROUND, 0), ^{
NSData* imgData = [NSData dataWithContentsOfURL:[dict objectForKey:@"URL"]];
UIImage* img = [UIImage imageWithData:imgData];
dispatch_async(dispatch_get_main_queue(), ^{
cell.albumLogo.image = img;
});
});
}
}
return cell;
}

-(void)tableView:(UITableView *)tableView didSelectRowAtIndexPath:(NSIndexPath *)indexPath
{
FacebookPhotoViewController* vc = [[FacebookPhotoViewController alloc] init];
vc.albumId = [[_datasource objectAtIndex:indexPath.row] objectForKey:@"id"];
vc.delegate = _delegate;
vc.title = [[_datasource objectAtIndex:indexPath.row] objectForKey:@"name"];
[self.navigationController pushViewController:vc animated:YES];
}


and here is FacebookPhotoViewController

- (void)viewDidLoad
{
[super viewDidLoad];

_datasource = [[NSMutableArray alloc] init];

[self sendRequest];

UICollectionViewFlowLayout* flow = [[UICollectionViewFlowLayout alloc] init];
self.collectionView = [[UICollectionView alloc] initWithFrame:self.view.bounds collectionViewLayout:flow];
self.collectionView.delegate = self;
self.collectionView.dataSource = self;

[self.collectionView registerClass:[UICollectionViewCell class] forCellWithReuseIdentifier:@"cell"];
[self.collectionView setBackgroundColor:[UIColor whiteColor]];

[self.view addSubview:self.collectionView];
// Do any additional setup after loading the view.
}
-(void)sendRequest {
NSString* graphPath = [NSString stringWithFormat:@"%@/photos?fields=created_time,name,picture&type=tagged", _albumId];

[FBRequestConnection startWithGraphPath:graphPath parameters:nil HTTPMethod:@"GET" completionHandler:^(FBRequestConnection *connection, id result, NSError *error) {
NSDictionary* resultDict = (NSDictionary*)result;

NSDictionary* dict = [resultDict objectForKey:@"photos"];
NSArray* array = [dict objectForKey:@"data"];
for (NSDictionary* innerDict in array) {
NSString* source = [innerDict objectForKey:@"source"];
[_datasource addObject:source];
}
[self.collectionView reloadData];
NSLog(@"Result is: %@",resultDict); **//Getting all pictures created date and id here**
}];
}

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

-(UICollectionViewCell *)collectionView:(UICollectionView *)collectionView cellForItemAtIndexPath:(NSIndexPath *)indexPath
{
static NSString* cellIdentifier = @"cell";
UICollectionViewCell* cell = [collectionView dequeueReusableCellWithReuseIdentifier:cellIdentifier forIndexPath:indexPath];

UIImageView* imageView = [[UIImageView alloc] initWithFrame:CGRectMake(0, 0, cell.contentView.frame.size.width, cell.contentView.frame.size.height)];
imageView.tag = 100;
imageView.contentMode = UIViewContentModeScaleAspectFill;
imageView.clipsToBounds = YES;
[cell.contentView addSubview:imageView];

dispatch_async(dispatch_get_global_queue(DISPATCH_QUEUE_PRIORITY_BACKGROUND, 0), ^{
NSData* imgData = [NSData dataWithContentsOfURL:[NSURL URLWithString:_datasource[indexPath.row]]];
UIImage* img = [UIImage imageWithData:imgData];
dispatch_async(dispatch_get_main_queue(), ^{
[imageView setImage:img];
});
});

return cell;
}

-(void)collectionView:(UICollectionView *)collectionView didSelectItemAtIndexPath:(NSIndexPath *)indexPath
{
if (_delegate) {
UICollectionViewCell* cell = [collectionView cellForItemAtIndexPath:indexPath];
UIImageView* imageView = (UIImageView*)[cell viewWithTag:100];
[_delegate faceBookViewController:self didSelectPhoto:imageView.image];
[self dismissViewControllerAnimated:YES completion:nil];
}
}


when i open FacebookPhotoViewController here is nothing just album name in navigation title that is it. please help

here is output
enter image description here

Answer

change this

NSString* graphPath = [NSString stringWithFormat:@"/%@?fields=photos", _albumId];

into and try once

NSString* graphPath = [NSString stringWithFormat:@"%@/photos", _albumId];

updated

 NSString* graphPath = [NSString stringWithFormat:@"%@/photos?fields=created_time,name,picture&type=tagged", _albumId];

updated -2

change this line

  NSString* source = [innerDict objectForKey:@"source"];

to

   NSString* source = [innerDict objectForKey:@"picture"]; 

detailed answer

      NSDictionary* resultDict = (NSDictionary*)result;
       NSArray* array = [resultDict objectForKey:@"data"];
 for (NSDictionary* innerDict in array) {
            NSString* source = [innerDict objectForKey:@"picture"]; 
            [_datasource addObject:source];
        }

   if (_datasource.count >0)
   {
    [self.collectionView reloadData];
   }
Comments