我已经实现了两个来自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
更新:您可以在此处查看Dropbox上的整个项目:https://www.dropbox.com/sh/hd4u8ef18z4m7ky/X30r7Z5l8l 更新#2:收藏视图现在以某种方式运作,我不知道我做了什么。感谢所有帮助过的人。
答案 0 :(得分:2)
我没有看到在任何地方设置的collectionView数据源或委托。
self.collectionView.dataSource = self;
self.collectionView.delegate = self;
答案 1 :(得分:0)
在下面进行以下操作:
- (void)didReceiveGroups:(NSArray *)groups
{
_groups = groups;
[_collectionView reloadData]; //using your instance here.
}
似乎在设置datasource之前你有了对象。在调用collectionView数据源方法之前,您可以尝试重新加载collectionView或重新构建所有组的代码。
请确保您在上述方法中拥有_groups的值。如果你在这里调试它应该说400个对象。