Reputation: 861
I have implemented two APIs from Steam that get the items from the players backpack, and match it with the GetSchema API to get the image URL and the description of each item. I displayed the information first on a table view and with cells, and it worked perfectly. Now, I have decided to display the information using a UICollectionView
, and the problem is that the code compiles perfectly, but the APIs aren't getting called and nothing is displaying. Here is my MasterViewController.h and .m files, which is the custom class of the 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
UPDATE: You can view the whole project on dropbox here: https://www.dropbox.com/sh/hd4u8ef18z4m7ky/X30r7Z5l8l
UPDATE #2: The Collection View somehow works now, I have no idea what I did. Thanks to everyone who helped.
Upvotes: 1
Views: 105
Reputation: 27187
Make this below:
- (void)didReceiveGroups:(NSArray *)groups
{
_groups = groups;
[_collectionView reloadData]; //using your instance here.
}
Seems you got object before datasource is set. You can try reload collectionView or rebuild your code where you got all groups before the collectionView datasource methods invoked.
Be sure that you have values for _groups in the method above. If you debug it here it should say 400 objects.
Upvotes: 0
Reputation: 13549
I don't see the collectionView datasource or delegate being set anywhere.
self.collectionView.dataSource = self;
self.collectionView.delegate = self;
Upvotes: 2