質問

Short background: I am creating an app in which the user can see the menus for each restaurant. I have created a class called "Dish" in which the name, ingredients and price of a menu item is set. I have then created a class for each restaurant which creates and stores the items for each restaurant. I want the UITableView, in which I display the menus, to divide the parts of the menus (for example "main dishes", desserts") into the table sections.

Ideally, I would create an array with the parts for each restaurant menu, in which a number of array (one for every menu part) containing the dishes in that part. I have tried this, and cannot get it to work. It seems like I cannot pass an array containing an array, since it will only return nil. Is this really the case?



Under is the code for my class containing the menu of the restaurant called Badholmen, which now only contains twi dishes. But other menus I have contain up to 70 dishes and there I need to be able to put every dish in an array, and the [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

To make the table view aware of the dishes in the menu, I call (in the init method of the table view):

    [[BadholmenStore sharedStore] emptyArray];
    [[BadholmenStore sharedStore] createBadholmen];
役に立ちましたか?

解決

Do like this, hope this helps u :)


   // 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 


ライセンス: CC-BY-SA帰属
所属していません StackOverflow
scroll top