0

J'ai travaillé sur ce projet pour élargir mon portfolio. Je suis encore un peu nouveau à l'objectif-c, donc l'aide serait grandement appréciée!NSArray a des objets mais UICollectionView apparaît vide

J'ai bricolé ce code depuis quelques jours maintenant. Je suis conscient que les étiquettes fonctionnent, car j'ai pu obtenir toutes les cellules pour afficher la même information (pas le résultat souhaité). Cela était probablement dû à un défaut où mon tableau ne stockait pas tous les objets.

Voici le code de mon UIViewController;

problème initial

J'ai donc confirmé que le NSLog, affiche 8 objets qui est ce que je vise.

@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]); 
} 

Cependant, lorsque j'utilise la méthode pour décider combien de cellules que je veux, par le nombre de tableau que je réalise 0 cellules sur le UICollectionView? Ce qui est déroutant puisque le NSLog confirme qu'il y a 8 objets.

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

Ceci est le code par rapport à la UIViewControllerCell et en attribuant les tableaux aux cellules UIObject (label/image) respective.

-(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; 
} 

Mes Soupçons

Je suis en train de sentir la logique derrière le problème. Et je ne suis pas sûr si cela revient au fait que les méthodes UICollectionView sont appelées avant viewDidLoad? Je veux dire dans le viewDidLoad le tableau évidemment à travers NSLog a 8 objets, mais quand on bricole avec les méthodes relatives à UICollectionView/Cells, ils semblent être vides ??

MISE À JOUR

est Ci-dessous le code qui se rapporte au dictionnaire à partir de laquelle le tableau est d'obtenir des informations affecté à;

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 
+0

Où est votre code qui remplit 'self.eventTitleArray'? Le code que vous avez publié remplit une autre variable locale, tableau, avec un nom similaire. – rmaddy

+0

Pourquoi avez-vous 5 tableaux séparés? Pourquoi pas un seul tableau d'objets d'événements? – rmaddy

+0

D'accord avec @rmaddy. Pensez à utiliser un tableau peuplé d'objets NSDictionary, contenant chacun ImageName, Titre, Emplacement, Prix et Type – Tcharni

Répondre

0

je me suis dit votre problème.

- (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]; 
... 
} 

Notez que vous re-déclarer toutes les variables à nouveau votre fonction viewDidLoad. Cela cache vos propriétés, et par conséquent, ils ne sont jamais vraiment initialisés. Une fois que vous les avez déclarés dans votre définition de classe, vous pouvez utiliser self.eventTitleArray = [[NSMutableArray alloc] init...] à la place.

Je suggère quelque chose comme:

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]); 
} 

Vous pouvez ensuite utiliser [NSDictionary objectForKey:@""] pour accéder au point de données nécessaires à une date ultérieure. Cela signifie également que vous ne devez gérer qu'un seul tableau contenant toutes vos informations. Qualitativement, cela n'affectera pas du tout la logique de votre programme

+0

Donc, dans votre fonction viewDidLoad: eventTitleArray et self.eventTitleArray sont deux variables différentes, d'où le problème – Tcharni

+0

Merci beaucoup SOOOOO !! Tu es un génie! Une solution aussi simple! Je vous remercie! Je vous remercie! JE VOUS REMERCIE! – FarisZ

+0

J'ai fait la même erreur plusieurs fois. Vous commencez simplement à faire attention à cela au-delà d'un point. – Tcharni