0

简短背景:我正在创建一个应用程序,用户可以在其中查看每家餐厅的菜单。我创建了一个名为“Dish”的类,其中设置了菜单项的名称、成分和价格。然后,我为每家餐厅创建了一个类,该类为每家餐厅创建和存储项目。我希望在其中显示菜单的 UITableView 将菜单的各个部分(例如“主菜”、甜点“)划分为表格部分。

理想情况下,我会为每个餐厅菜单创建一个包含部分的数组,其中包含该部分中的菜肴的多个数组(每个菜单部分一个)。我已经尝试过了,但无法让它工作。似乎我无法传递包含数组的数组,因为它只会返回 nil。真的是这样吗?



下面是我班级的代码,其中包含名为 Badholmen 的餐厅的菜单,该餐厅现在只包含两道菜。但是我的其他菜单最多包含 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];
4

1 回答 1

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 


于 2013-09-03T10:26:01.423 回答