NSArray 有对象,但 UICollectionView 显示为空 [英] NSArray has objects yet UICollectionView is appearing empty

查看:41
本文介绍了NSArray 有对象,但 UICollectionView 显示为空的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我一直致力于这个项目以扩展我的投资组合.我对objective-c还是比较陌生,所以非常感谢帮助!

I've been working on this project to expand my portfolio. I am still rather new to objective-c, so help would be highly appreciated!

几天来我一直在修改这段代码.我知道标签有效,因为我能够让所有单元格显示相同的信息(不是所需的结果).这可能是我的数组没有存储所有对象的错误.

I have been tinkering with this code for a few days now. I am aware the labels work, as I was able to get all the cells to display the same information (not the desired result). This was likely to a fault where my array didn't store all the objects.

下面是我的 UIViewController 的代码;

Below is the code for my UIViewController;

初始问题

所以我已经确认 NSLog 显示 8 个对象,这正是我的目标.

So I have confirmed that the NSLog, displays 8 objects which is what I aiming for.

@interface YourDowey ()

@property (nonatomic, strong) NSMutableArray *eventTitleArray;
@property (nonatomic, strong) NSMutableArray *eventLocationArray;
@property (nonatomic, strong) NSMutableArray *eventIconArray;
@property (nonatomic, strong) NSMutableArray *eventPriceArray;
@property (nonatomic, strong) NSMutableArray *eventTypeArray;


@end

@implementation YourDowey

- (void)viewDidLoad {
[super viewDidLoad];

NSMutableArray *eventTitleArray = [[NSMutableArray alloc]initWithCapacity:8];
NSMutableArray *eventLocationArray = [[NSMutableArray alloc]initWithCapacity:8];
NSMutableArray *eventIconArray = [[NSMutableArray alloc]initWithCapacity:8];
NSMutableArray *eventPriceArray = [[NSMutableArray alloc]initWithCapacity:8];
NSMutableArray *eventTypeArray = [[NSMutableArray alloc]initWithCapacity:8];

for (NSUInteger index = 0; (index < 8) ; index++){

    EventsList *eventList = [[EventsList alloc] initWithIndex:index];

    NSString *individualEventTitle = eventList.eventTitle;
    NSString *individualEventLocation = eventList.eventLocation;

    NSString *individualEventIcon = eventList.eventIcon;
    NSString *individualEventPrice = eventList.eventPrice;
    NSString *individualEventType = eventList.eventType;

    [eventTitleArray addObject:individualEventTitle];
    [eventLocationArray addObject:individualEventLocation];
    [eventIconArray addObject:individualEventIcon];
    [eventPriceArray addObject:individualEventPrice];
    [eventTypeArray addObject:individualEventType];

    }
NSLog(@"Events: %@", eventTitleArray);
NSLog(@"Number of objects: %lu", (unsigned long)[eventTitleArray count]);
}

但是,当我使用该方法决定我想要多少个单元格时,通过数组计数我在 UICollectionView 上实现了 0 个单元格?这令人困惑,因为 NSLog 确认有 8 个对象.

However, when I use the method for deciding how many cells I want, through the array count I achieve 0 cells on the UICollectionView? Which is confusing as the NSLog confirms that there are 8 objects.

-(NSInteger)collectionView:(UICollectionView *)collectionView numberOfItemsInSection:(NSInteger)section{
return [self.eventTitleArray count];
}

这是与 UIViewControllerCell 相关的代码,并将数组分配给各个 UIObject(标签/图像)的单元格.

This is the code relative to the UIViewControllerCell and assigning the arrays to the cells respective UIObject (label/image).

-(UICollectionViewCell *)collectionView:(UICollectionView *)collectionView cellForItemAtIndexPath:(NSIndexPath *)indexPath{
EventsCell *cell = [collectionView dequeueReusableCellWithReuseIdentifier:@"eventsCell" forIndexPath:indexPath];

NSString *imagesString = [self.eventIconArray objectAtIndex:indexPath.row];

cell.eventImage.image = [UIImage imageNamed:imagesString];
cell.eventTitle.text = [self.eventTitleArray objectAtIndex:indexPath.row];
cell.eventLocation.text = [self.eventLocationArray objectAtIndex:indexPath.row];
cell.eventPrice.text = [self.eventPriceArray objectAtIndex:indexPath.row];
cell.eventType.text = [self.eventTypeArray objectAtIndex:indexPath.row];

return cell;
}

我的怀疑

我正在尝试了解问题背后的逻辑.而且我不确定这是否归结为在 viewDidLoad 之前调用 UICollectionView 方法的事实?我的意思是在 viewDidLoad 数组中显然通过 NSLog 有 8 个对象,但是当修改与 UICollectionView/Cells 相关的方法时,它们似乎是空的??

I'm trying to sense the logic behind the problem. And I'm not sure if it goes down to the fact the UICollectionView methods are called before the viewDidLoad? I mean in the viewDidLoad the array evidently through NSLog has 8 objects in it, yet when tinkering with the methods relative to the UICollectionView/Cells they seem to be empty??

更新

下面是与 Array 从中获取分配信息的字典相关的代码;

Below is the code which relates to the dictionary from which the Array is getting assigned information to;

EventsLibrary.h

#import <Foundation/Foundation.h>

extern NSString *const kTitle;
extern NSString *const kLocation;
extern NSString *const kPrice;
extern NSString *const kIcon;
extern NSString *const kLargeIcon;
extern NSString *const kType;

@interface EventsLibrary : NSObject

@property (strong, nonatomic) NSArray *library;

@end

EventsLibrary.m

#import "EventsLibrary.h"

NSString *const kTitle = @"title";
NSString *const kLocation = @"location";
NSString *const kPrice = @"price";
NSString *const kIcon = @"icon";
NSString *const kLargeIcon = @"largeIcon";
NSString *const kType = @"type";

@implementation EventsLibrary

-(instancetype) init{
self = [super init];
if (self) {
    _library        =   @[      @{   kTitle:@"iCafe de Paris",
                                     kLocation:@"International Drive",
                                     kPrice:@"$10",
                                     kIcon:@"iCafe.png",
                                     kLargeIcon:@"iCafeLrg.png",
                                     kType:@"Food"},

                                @{   kTitle:@"Orlando's Museum of Art",
                                     kLocation:@"N Mills Ave",
                                     kPrice:@"$20",
                                     kIcon:@"Museum.png",
                                     kLargeIcon:@"MuseumLrg.png",
                                     kType:@"Art"},

                                @{   kTitle:@"Club 180",
                                     kLocation:@"W Church Street",
                                     kPrice:@"$20",
                                     kIcon:@"Club180.png",
                                     kLargeIcon:@"Club180Lrg.png",
                                     kType:@"NightLife"},

                                @{   kTitle:@"Wekiva Springs",
                                     kLocation:@"Wekiva Circle, Apopka",
                                     kPrice:@"$5",
                                     kIcon:@"Wekiva.png",
                                     kLargeIcon:@"WekivaLrg.png",
                                     kType:@"Nature"},

                                @{   kTitle:@"Kings Bowling",
                                     kLocation:@"International Drive",
                                     kPrice:@"$10",
                                     kIcon:@"Kings.png",
                                     kLargeIcon:@"KingLrg.png",
                                     kType:@"Sports"},

                                @{   kTitle:@"Pirate's Cove Mini Golf",
                                     kLocation:@"International Drive",
                                     kPrice:@"$15",
                                     kIcon:@"PiratesGolf.png",
                                     kLargeIcon:@"PiratesGolfLrg.png",
                                     kType:@"Sports"},

                                @{   kTitle:@"Cobb's Plaza Cinema",
                                     kLocation:@"S Orange Ave",
                                     kPrice:@"$8",
                                     kIcon:@"Cobbs.png",
                                     kLargeIcon:@"CobbsLrg.png",
                                     kType:@"Art"},

                                @{   kTitle:@"Mango's Cafe",
                                     kLocation:@"International Drive",
                                     kPrice:@"FREE",
                                     kIcon:@"Mangos.png",
                                     kLargeIcon:@"MangosLrg.png",
                                     kType:@"Food"}
                          ];
}
return self;
}

@end

EventsList.h

#import <Foundation/Foundation.h>
#import <UIKit/UIKit.h>
#import "EventsLibrary.h"

@interface EventsList : NSObject

@property (strong, nonatomic) NSString *eventTitle;
@property (strong, nonatomic) NSString *eventLocation;
@property (strong, nonatomic) NSString *eventPrice;
@property (strong, nonatomic) NSString *eventIcon;
@property (strong, nonatomic) NSString *eventIconLarge;
@property (strong, nonatomic) NSString *eventType;

- (instancetype)initWithIndex:(NSUInteger)index;

@end

EventsList.m

#import "EventsList.h"

@implementation EventsList

-(instancetype)initWithIndex:(NSUInteger)index{
self = [super init];
if (self) {

    EventsLibrary *eventsLibrary = [[EventsLibrary alloc]init];
    NSArray *library = eventsLibrary.library;

    NSDictionary *eventsDictionary = library[index];

    _eventTitle = [eventsDictionary objectForKey:kTitle];
    _eventLocation = [eventsDictionary objectForKey:kLocation];
    _eventPrice = [eventsDictionary objectForKey:kPrice];

    NSString *iconName = [eventsDictionary objectForKey:kIcon];
    _eventIcon = [UIImage imageNamed:iconName];

    NSString *largeIconName = [eventsDictionary objectForKey:kLargeIcon];
    _eventIconLarge = [UIImage imageNamed:largeIconName];

    _eventType = [eventsDictionary objectForKey:kType];
}
return self;
}

@end

推荐答案

我发现了你的问题.

- (void)viewDidLoad {
[super viewDidLoad];

NSMutableArray *eventTitleArray = [[NSMutableArray alloc]initWithCapacity:8];
NSMutableArray *eventLocationArray = [[NSMutableArray alloc]initWithCapacity:8];
NSMutableArray *eventIconArray = [[NSMutableArray alloc]initWithCapacity:8];
NSMutableArray *eventPriceArray = [[NSMutableArray alloc]initWithCapacity:8];
NSMutableArray *eventTypeArray = [[NSMutableArray alloc]initWithCapacity:8];
...
}

请注意,您将再次在 viewDidLoad 函数中重新声明所有变量.这会隐藏您的属性,因此它们从未真正初始化.一旦在类定义中声明它们,就可以使用 self.eventTitleArray = [[NSMutableArray alloc] init...] 代替.

Note that you are re-declaring all the variables again in your viewDidLoad function. This hides your properties, and hence they are never really initialised. Once you declare them in your class definition, you can use self.eventTitleArray = [[NSMutableArray alloc] init...] instead.

我建议如下:

NSMutableArray *eventArray; //One single array for all

- (void)viewDidLoad {
[super viewDidLoad];

self.eventArray = [[NSMutableArray alloc]initWithCapacity:8];

for (NSUInteger index = 0; (index < 8) ; index++){

    EventsList *eventList = [[EventsList alloc] initWithIndex:index];

    NSString *individualEventTitle = eventList.eventTitle;
    NSString *individualEventLocation = eventList.eventLocation;

    NSString *individualEventIcon = eventList.eventIcon;
    NSString *individualEventPrice = eventList.eventPrice;
    NSString *individualEventType = eventList.eventType;

    NSDictionary *eventDictionary = [[NSDictionary alloc] initWithObjectsAndKeys: eventList.eventTitle, @"Title", eventList.eventLocation, @"Location", eventList.eventIcon, @"Icon", eventList.eventPrice, @"Price, eventList.eventType, @"Type", nil];

    [eventArray addObject:eventDictionary];

    }
NSLog(@"Events: %@", eventArray);
NSLog(@"Number of objects: %lu", (unsigned long)[eventArray count]);
}

然后您可以使用 [NSDictionary objectForKey:@""] 以在将来访问所需的数据点.这也意味着,您只需管理一个包含您所有信息的阵列.定性的,它根本不会影响你的程序逻辑

You could then use [NSDictionary objectForKey:@""] to access the required data point at a future time. It also means, you only have to manage one array that has all your information. Qualitatively, it will not affect your program logic at all

这篇关于NSArray 有对象,但 UICollectionView 显示为空的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆