首页
学习
活动
专区
圈层
工具
发布
社区首页 >问答首页 >NSArray有对象,但UICollectionView显示为空

NSArray有对象,但UICollectionView显示为空
EN

Stack Overflow用户
提问于 2017-07-17 03:21:17
回答 1查看 122关注 0票数 0

我一直在做这个项目来扩大我的投资组合。我对目标-c还很陌生,所以我们会非常感谢你的帮助!

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

下面是我的UIViewController代码;

初始问题

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

代码语言:javascript
复制
@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个对象。

代码语言:javascript
复制
-(NSInteger)collectionView:(UICollectionView *)collectionView numberOfItemsInSection:(NSInteger)section{
return [self.eventTitleArray count];
}

这是相对于UIViewControllerCell的代码,并将数组分配给单元格各自的UIObject (标签/图像)。

代码语言:javascript
复制
-(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;
}

我的怀疑

我在试着理解问题背后的逻辑。我不确定是否可以归结为UICollectionView方法是在viewDidLoad之前调用的事实?我的意思是,在viewDidLoad中,数组显然通过NSLog有8个对象,但是当修改相对于UICollectionView/单元的方法时,它们似乎是空的?

更新

下面是与数组获得分配信息的字典相关的代码;

EventsLibrary.h

代码语言:javascript
复制
#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

代码语言:javascript
复制
#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

代码语言:javascript
复制
#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

代码语言:javascript
复制
#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
EN

回答 1

Stack Overflow用户

回答已采纳

发布于 2017-07-17 03:39:20

我解决了你的问题。

代码语言:javascript
复制
- (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...]

我建议如下:

代码语言:javascript
复制
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:@""]访问所需的数据点。这也意味着,您只需管理一个包含所有信息的数组。从质量上讲,它根本不会影响您的程序逻辑。

票数 0
EN
页面原文内容由Stack Overflow提供。腾讯云小微IT领域专用引擎提供翻译支持
原文链接:

https://stackoverflow.com/questions/45135501

复制
相关文章

相似问题

领券
问题归档专栏文章快讯文章归档关键词归档开发者手册归档开发者手册 Section 归档