自定义 UICollectionViewCell 未加载

Custom UICollectionViewCell not loading

我正在使用故事板,但我的自定义 UICollectionViewCell 没有出现。我玩了几个小时,用谷歌搜索了很多不同的解决方案,但 none 有效。澄清一下,数据存在,并且出现了 UICollectionView,但单元格不存在。这是我的代码。如有任何建议,我们将不胜感激!

- (NSInteger)collectionView:(UICollectionView *)mutualFriendsView numberOfItemsInSection:(NSInteger)section {
return [self.resultsDictionary count];
}

- (UICollectionViewCell *)collectionView:(UICollectionView *)mutualFriendsView
              cellForItemAtIndexPath:(NSIndexPath *)indexPath{
static NSString *identifier = @"PCRRequesterMutualFriendsCollectionViewCell";

PCRRequesterMutualFriendsCollectionViewCell *cell = [self.mutualFriendsView dequeueReusableCellWithReuseIdentifier:identifier forIndexPath:indexPath];


NSArray *idArray = [self.resultsDictionary objectForKey:@"id"];
NSArray *nameArray = [self.resultsDictionary objectForKey:@"name"];

cell.profileName.text = [nameArray objectAtIndex:indexPath.row];
cell.backgroundColor = [UIColor redColor];

return cell;
}

- (void)collectionView:(UICollectionView *)mutualFriendsView didSelectItemAtIndexPath:(NSIndexPath *)indexPath
{
NSLog(@"cell #%d was selected", indexPath.row);
}

- (BOOL)shouldAutorotate {
[mutualFriendsView.collectionViewLayout invalidateLayout];

BOOL retVal = YES;
return retVal;
}

编辑 这是我的 viewDidLoad

self.mutualFriendsView.dataSource = self;
self.mutualFriendsView.delegate = self;

self.mutualFriendsView.pagingEnabled = YES;

//    [self.mutualFriendsView registerClass:[PCRMutualFriendsCollectionViewCell class] forCellWithReuseIdentifier:@"PCRMutualFriendsCollectionViewCell"];

编辑 我想我找到了问题所在。我认为完成块完成后不会填充字典。关于从块中保存字典值以在块外使用的任何建议?

__block NSMutableDictionary *mutualFriends = nil;

__block NSNumber *total;

NSString *u = [NSString stringWithFormat:@"%@",self.details[@"Requester"][@"profile"][@"facebookId"]];
/* make the API call */
[FBRequestConnection startWithGraphPath:(@"/%@", u)
                             parameters:params
                             HTTPMethod:@"GET"
                      completionHandler:^(FBRequestConnection *connection, id result, NSError *error) {
                          /* handle the result */
                          if (!error){
                              NSLog(@"RESULT OF FB %@", result);
                              if (result == nil){
                                  NSLog(@"No shared friends");
                              } else {
                                  total = result[@"context"][@"mutual_friends"][@"summary"][@"total_count"];
                                  NSLog(@"TOTAL FRIENDS %@", total);
                                  for(int i=0;i<[result[@"context"][@"mutual_friends"][@"data"] count];i++)
                                  {
                                      mutualFriends = result[@"context"][@"mutual_friends"][@"data"][i];


                                      NSLog(@"FRIENDDATA %@", mutualFriends);
                                  }

                              }


                          } else {
                              NSLog(@"ERROR %@", error);
                          }

                      }];



self.resultsDictionary = mutualFriends;
self.number = total;
NSLog(@"NUMBER %@", self.number);
NSLog(@"RESULTS DICTIONARY %@", self.resultsDictionary);
NSString *friends = [NSString stringWithFormat:@"You have %@ friends in common including:", self.number];

此代码后:

for(int i=0;i<[result[@"context"][@"mutual_friends"][@"data"] count];i++)
{
   mutualFriends = result[@"context"][@"mutual_friends"][@"data"][i];
   NSLog(@"FRIENDDATA %@", mutualFriends);
}
// add
self.resultsDictionary = mutualFriends;
mutualFriendsView.reloadData();

全部在该完成块内。因此,当 FB 最终执行 return 并且您已经积累了所有 mutualFriends 时,然后您告诉 collectionView 重新加载。