UICollection视图未加载



我从Steam中实现了两个api,它们从玩家背包中获取物品,并将其与GetSchema api进行匹配,以获取图像url和每个物品的描述。我首先在表格视图和单元格中显示了信息,效果非常好。现在,我决定使用UICollectionView显示信息,问题是代码编译得很好,但没有调用api,也没有显示任何内容。这是我的MasterViewController.h和.m文件,它是UICollectionView的自定义类。

MasterViewController.h

#import <UIKit/UIKit.h> @interface MasterViewController : UICollectionViewController @end

MasterViewController.m

#import "MasterViewController.h"
#import "Group.h"
#import "Item.h"
#import "SteamManager.h"
#import "SteamCommunicator.h"
#import "backpackIcons.h"

@interface MasterViewController () <SteamManagerDelegate> {
 NSArray *_groups;
NSArray *_itemGroups;
NSArray *_backpackItems;
NSArray *_backpackItemPhotos;
SteamManager *_manager;
}
@end
@implementation MasterViewController
- (void)viewDidLoad
{
[super viewDidLoad];
_manager = [[SteamManager alloc] init];
_manager.communicator = [[SteamCommunicator alloc] init];
_manager.communicator.delegate = _manager;
_manager.delegate = self;
NSLog(@"Starting");
[self startFetchingGroups];
}
#pragma mark - Creating Backpack Icons
-(NSArray *)createBackpackIcons:(NSArray *)groups usingItemGroups:(NSArray *)items
{
    NSMutableArray *backpackItems = [[NSMutableArray alloc] init];
    for (NSInteger i = 0; i < _groups.count; i++) {
    Group *group = _groups[i];
    NSString *defindex1 = [NSString stringWithFormat:@"%@", group.defindex];
    for (NSInteger j = 0; j < _itemGroups.count; j++)
    {
        Item *item = _itemGroups[j];
        NSString *defindex2 = [NSString stringWithFormat:@"%@", item.defindex];
        if([defindex1 isEqualToString:defindex2])
        {
            NSLog(@"%@", item.name);
            backpackIcons *backpack = [[backpackIcons alloc] init];
            backpack.name = item.name;
            backpack.original_id = group.original_id;
            backpack.defindex = item.defindex;
            backpack.level = group.level;
            backpack.quality = group.quality;
            backpack.image_url = item.image_url;
            backpack.item_description = item.item_description;
            [backpackItems addObject:backpack];
        }
    }
}
return backpackItems;
}
#pragma mark - Notification Observer
- (void)startFetchingGroups
{
    [_manager fetchGroups];
}
#pragma mark - SteamManagerDelegate
- (void)didReceiveGroups:(NSArray *)groups
{
    _groups = groups;
}
- (void)didReceieveItemGroups:(NSArray *)groups
{
    _itemGroups = groups;
    _backpackItems = [self createBackpackIcons:_groups
                           usingItemGroups:_itemGroups];
dispatch_async(dispatch_get_main_queue(), ^{
    [self.collectionView reloadData];
});
}
- (void)fetchingGroupsFailedWithError:(NSError *)error
{
NSLog(@"Error %@; %@", error, [error localizedDescription]);
}
#pragma mark - Collection View
- (NSInteger)collectionView:(UICollectionView *)collectionView numberOfItemsInSection:   (NSInteger)section {
    return _groups.count;
}

- (UICollectionViewCell *)collectionView:(UICollectionView *)collectionView cellForItemAtIndexPath:(NSIndexPath *)indexPath{
static NSString *identifier = @"Cell";
backpackIcons *item = _backpackItems[indexPath.row];
NSString *photoURL = item.image_url;
UICollectionViewCell *cell = [collectionView dequeueReusableCellWithReuseIdentifier:identifier forIndexPath:indexPath];
UIImageView *itemImageView = (UIImageView *)[cell viewWithTag:100];
itemImageView.image = [UIImage imageWithData:[NSData dataWithContentsOfURL:[NSURL URLWithString:photoURL]]];
return cell;
}
/*
#pragma mark - Table View
- (NSInteger)tableView:(UITableView *)tableView numberOfRowsInSection:(NSInteger)section
{
return _groups.count;
}
- (UITableViewCell *)tableView:(UITableView *)tableView cellForRowAtIndexPath:(NSIndexPath *)indexPath
{
DetailCell *cell = [tableView dequeueReusableCellWithIdentifier:@"Cell" forIndexPath:indexPath];
Group *group = _groups[indexPath.row];
Item *group2 = _itemGroups[indexPath.row];
backpackIcons *group3 = _backpackItems[indexPath.row];
[cell.nameLabel setText:[NSString stringWithFormat:@"%@", group3.level]];
[cell.originalidLabel setText:[NSString stringWithFormat:@"%@", group3.original_id]];
[cell.qualityLabel setText:[NSString stringWithFormat:@"%@", group3.image_url]];
[cell.levelLabel setText:[NSString stringWithFormat:@"%@", group3.item_description]];
return cell;
}
*/
@end

更新:您可以在下拉框中查看整个项目:https://www.dropbox.com/sh/hd4u8ef18z4m7ky/X30r7Z5l8l更新#2:集合视图现在以某种方式工作,我不知道我做了什么。感谢所有提供帮助的人

我没有看到collectionView数据源或委托在任何地方被设置。

self.collectionView.dataSource = self;
self.collectionView.delegate = self;

如下所示:

- (void)didReceiveGroups:(NSArray *)groups
{
    _groups = groups;
   [_collectionView reloadData]; //using your instance here.
}

似乎在设置数据源之前就得到了对象。您可以尝试重新加载collectionView或在调用collectionView数据源方法之前获得所有组的位置重新生成代码。

请确保在上面的方法中有_groups的值。如果你在这里调试它,它应该是400个对象。

最新更新