简短背景:我正在创建一个应用程序,用户可以在其中查看每个餐厅的菜单。我创建了一个名为“Dish”的类,其中设置了菜单项的名称,成分和价格。然后我为每个餐厅创建了一个课程,为每个餐厅创建和存储项目。我想要UITableView,我在其中显示菜单,将菜单的部分(例如“主菜”,甜点“)分成表格部分。
理想情况下,我会创建一个包含每个餐厅菜单的部分的数组,其中包含该部分菜肴的多个数组(每个菜单部分一个)。我试过这个,但无法让它发挥作用。好像我无法传递包含数组的数组,因为它只返回nil。这是真的吗?
Under是我班级的代码,其中包含名为Badholmen的餐厅菜单,现在只包含twi菜肴。但是我的其他菜单包含多达70种菜肴,我需要能够将每道菜放入一个阵列中,[allBadholmen addObject:ArrayOfMenuParts
:
#import "BadholmenStore.h"
#import "Dish.h"
@implementation BadholmenStore
+ (BadholmenStore *)sharedStore
{
static BadholmenStore *sharedStore = nil;
if (!sharedStore)
sharedStore = [[super allocWithZone:nil] init];
return sharedStore;
}
#pragma mark - tillägg av platser
- (void)createBadholmen
{
Dish *bhvar = [[Dish alloc] initWithName:@"Varierande meny"
ingredients:@" "
price:@" "];
[allBadholmen addObject:bhvar];
Dish *bhsom = [[Dish alloc] initWithName:@"Sommarlunch med varierande meny (endast sommartid)"
ingredients:@"inkl. måltidsdryck"
price:@"79:-"];
[allBadholmen addObject:bhsom];
}
- (void)emptyArray
{
[allBadholmen removeAllObjects];
}
- (NSArray *)allBadholmen
{
return allBadholmen;
}
#pragma mark - Overriden methods
+ (id)allocWithZone:(NSZone *)zone
{
return [self sharedStore];
}
- (id)init
{
self = [super init];
if (self) {
allBadholmen = [[NSMutableArray alloc] init];
}
return self;
}
@end
为了让表视图知道菜单中的菜肴,我调用(在表视图的init方法中):
[[BadholmenStore sharedStore] emptyArray];
[[BadholmenStore sharedStore] createBadholmen];
答案 0 :(得分:1)
这样做,希望这有助于你:)
// in BadholmenStore.h
#import <Foundation/Foundation.h>
@interface BadholmenStore : NSObject
{
NSMutableArray *allBadholmen; //for your shared object contains one "mutable array", define it hear
}
+ (BadholmenStore *)sharedStore; //your class method
//instance methods
- (void)createBadholmen;
- (void)emptyArray;
- (NSMutableArray *)allBadholmen;
@end
// in BadholmenStore.m
#import "BadholmenStore.h"
#import "Dish.h"
@implementation BadholmenStore
static BadholmenStore *sharedStore = nil; //it shoud be visible to all, put this line hear
+ (BadholmenStore *)sharedStore
{
if (!sharedStore)
sharedStore = [[super allocWithZone:nil] init];
return sharedStore;
}
#pragma mark - tillägg av platser
- (void)createBadholmen
{
Dish *bhvar = [[Dish alloc] initWithName:@"Varierande meny"
ingredients:@" "
price:@" "];
[allBadholmen addObject:bhvar];
Dish *bhsom = [[Dish alloc] initWithName:@"Sommarlunch med varierande meny (endast sommartid)" ingredients:@"inkl. måltidsdryck" price:@"79:-"];
[allBadholmen addObject:bhsom];
}
- (void)emptyArray
{
[allBadholmen removeAllObjects];
}
- (NSMutableArray *)allBadholmen // replace your NSArray with NSMutableArray
{
return allBadholmen;
}
#pragma mark - Overriden methods
+ (id)allocWithZone:(NSZone *)zone
{
return [self sharedStore];
}
- (id)init
{
self = [super init];
if (self) {
allBadholmen = [[NSMutableArray alloc] init];
}
return self;
}
@end
//in the class where u are using this shared class object
[super viewDidLoad];
[[BadholmenStore sharedStore] emptyArray];
[[BadholmenStore sharedStore] createBadholmen];
NSMutableArray *allObjects = [[BadholmenStore sharedStore] allBadholmen]; // now use allObjects