Facebook 在 uitableview 中加载相册,但未在下一个收藏视图中打开

Posted

技术标签:

【中文标题】Facebook 在 uitableview 中加载相册,但未在下一个收藏视图中打开【英文标题】:Facebook loading albums in uitableview but not opening in next collection view 【发布时间】:2016-08-12 09:36:38 【问题描述】:

我正在尝试访问 facebook 相册,我在我的应用程序中实现了它,但是当我运行它时。当我选择相册时,它显示所有相册但不显示这些相册中的图片。这是我的代码 注意:albumsvi​​ewcontroller 有一个 xib,但 FacebookPhotoViewController 没有 xib,所以我需要创建一个吗?

这是来自 albumsvi​​ewcontroller 的 cellforraw 方法

-(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];

这里是 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];
        
    

当我在这里打开 FacebookPhotoViewController 时,导航标题中的专辑名称就是它。请帮忙

这里是输出

【问题讨论】:

你得到_albumId,如果是,你会得到这里resultDict i nslog resultdict 但没有输出! 【参考方案1】:

改一下

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

进入并尝试一次

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

更新

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

更新 -2

改变这一行

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

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

详细解答

      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];
   

【讨论】:

太好了,现在我在 resultDict 中得到了所有图像 ID 的输出,但没有显示! @VixHunk - 很高兴听到,我之前没有使用过这个概念,好的,你能用resultDict更新问题吗 现在尝试了解所有细节,包括照片但没有显示任何内容! @VixHunk - 真的很高兴你能显示结果的输出 你在这里得到什么输出 _datasourcedict

以上是关于Facebook 在 uitableview 中加载相册,但未在下一个收藏视图中打开的主要内容,如果未能解决你的问题,请参考以下文章

在 UIView 中加载 UITableView

无法在 Playground 中加载 UITableView

在 UITableView 中加载图像时遇到问题

在 coredata 中加载图像时 UITableView 滞后

尝试在 UITableView 中加载大量 JSON 数据需要很长时间

像 Facebook 应用一样的 UITableView