我一直在这个项目上工作,以扩大我的投资组合。我还是比较新的Objective-C,所以帮助将不胜感激!NSArray有对象但UICollectionView显示为空
我一直在修改这段代码几天。我知道这些标签是有用的,因为我能够让所有的单元显示相同的信息(不是理想的结果)。这可能是我的阵列没有存储所有对象的错误。
下面是我的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类(标签/图像)。
-(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
你的代码在哪里填充'self.eventTitleArray'?您发布的代码使用相似的名称填充另一个本地数组变量。 – rmaddy
为什么你有5个独立的阵列?为什么不只是一个事件对象数组? – rmaddy
同意@rmaddy。考虑使用一个填充NSDictionary对象的数组,每个对象包含ImageName,Title,Location,Price和Type – Tcharni