Objective c NSArray有对象,但UICollectionView显示为空

Objective c NSArray有对象,但UICollectionView显示为空,objective-c,uicollectionview,nsarray,uicollectionviewcell,Objective C,Uicollectionview,Nsarray,Uicollectionviewcell,我一直在做这个项目来扩大我的投资组合。我对objective-c还比较陌生,所以非常感谢您的帮助 我已经修改这个代码好几天了。我知道标签是有效的,因为我能够让所有单元格显示相同的信息(不是期望的结果)。这可能是因为我的数组没有存储所有对象 下面是我的UIViewController的代码 初始问题 所以我已经确认NSLog显示了8个对象,这正是我的目标 @interface YourDowey () @property (nonatomic, strong) NSMutableArray *e

我一直在做这个项目来扩大我的投资组合。我对objective-c还比较陌生,所以非常感谢您的帮助

我已经修改这个代码好几天了。我知道标签是有效的,因为我能够让所有单元格显示相同的信息(不是期望的结果)。这可能是因为我的数组没有存储所有对象

下面是我的UIViewController的代码

初始问题

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

@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]);
}
这是相对于UIViewControllerCell的代码,并将数组分配给相应UIObject(标签/图像)的单元格

我的怀疑

我正试图理解问题背后的逻辑。我不确定这是否是因为UICollectionView方法是在viewDidLoad之前调用的?我的意思是,在viewDidLoad中,显然通过NSLog加载的数组中有8个对象,但是当修补与UICollectionView/单元格相关的方法时,它们似乎是空的

已更新

下面是与数组从中获取指定信息的字典相关的代码

事件图书馆.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
#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
事件列表.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
#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

我解决了你的问题

- (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…]

我想提出如下建议:

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]);
}
NSMutableArray*eventArray//一个阵列用于所有
-(无效)viewDidLoad{
[超级视图下载];
self.eventArray=[[NSMutableArray alloc]initWithCapacity:8];
对于(整数索引=0;(索引<8);索引++){
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(@“事件:%@”,事件数组);
NSLog(@“对象数:%lu”,(无符号长)[eventArray计数];
}

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

填充self.eventTitleArray的代码在哪里?您发布的代码用类似的na填充另一个本地数组变量我。为什么你有5个独立的数组?为什么不只是一个事件对象数组?同意@ RMIDE。考虑使用NS字典对象填充的一个数组,每个都包含VIELDDIAD函数中的图像名、标题、位置、价格和类型:EvithTimeRead和Self.EvestTimeRead是两个不同的变量,因此问题是你真是太棒了!!你真是个天才!这么简单的解决方案!谢谢!谢谢!谢谢!我已经犯了很多次同样的错误。你只是开始小心了。
- (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];
...
}
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]);
}