自定义UICollectionViewCell无法加载

时间:2015-02-09 22:06:03

标签: ios objective-c uicollectionview uicollectionviewcell

我正在使用故事板,我的自定义UICollectionViewCell没有出现。我玩了几个小时,用谷歌搜索了大量不同的解决方案,但都没有奏效。只是为了澄清,数据存在,并且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];

1 个答案:

答案 0 :(得分:0)

在此代码之后:

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最终返回并且您已经累积了所有的mutualFriends时,那么您告诉collectionView重新加载。