gpt4 book ai didi

ios - UICollectionView 未加载

转载 作者:行者123 更新时间:2023-11-29 10:44:00 24 4
gpt4 key购买 nike

我已经实现了两个来自 Steam 的 api,它们从玩家背包中获取元素,并将其与 GetSchema api 匹配以获取图像 url 和每个元素的描述。我首先在表格 View 和单元格中显示信息,并且效果很好。现在,我决定使用 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: Collection View 现在可以正常工作了,我不知道我做了什么。感谢所有提供帮助的人。

最佳答案

我没有看到任何地方设置了 collectionView 数据源或委托(delegate)。

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

关于ios - UICollectionView 未加载,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/23096668/

24 4 0
Copyright 2021 - 2024 cfsdn All Rights Reserved 蜀ICP备2022000587号
广告合作:1813099741@qq.com 6ren.com