objective-c - uicollectionview教學 - uicollectionview uicollectionviewcell



NSArray有對象,但UICollectionView顯示為空 (1)

我一直在做這個項目來擴大我的投資組合。 我還是比較新的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]);
}

但是,當我使用該方法來決定我想要多少個單元格時,通過數組計數,我在UICollectionView上實現了0個單元格? NSLog確認有8個對象,這是令人困惑的。

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

這是相對於UIViewControllerCell的代碼,並將數組分配給各個單元的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的方法,他們似乎是空的?

更新

下面是代碼,它涉及到數組獲取分配信息的字典;

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...]來代替。

我會建議像這樣的東西:

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:@""]在將來訪問所需的數據點。 這也意味著,你只需要管理一個包含所有信息的數組。 定性地說,它不會影響你的程序邏輯





uicollectionviewcell