0

I created a menu app in Xcode. I have retrieved my menus from a JSON file.

"Mon": [
   {
   "Type" : "Breakfast",
   "Name":"Sweet Potato Breakfast Taquitos",
   "Price" : "OMR 1.500",
    },
    {   
   "Type" : "Dinner",
   "Name":"Fall Sausage Skillet Dinner",
   "Price" : "OMR 1.300",
   }
       ],

"Sun": [

   {
   "Type" : "Breakfast",
   "Name":"Breakfast Casserole",
   "Price" : "1500",
   },
   {
   "Type" : "Breakfast",
   "Name":"Breakfast Tart",
   "Price" : "2000", 
   },]

In my json.h I have this line code which I will use in in my UITableView:

-(void)dataRequestCompletedWithJsonObject:(id)jsonObject;

Then I call this method in my UITableView.

///Table view delegate

- (void)tableView:(UITableView *)tableView didSelectRowAtIndexPath:(NSIndexPath *)indexPath
{
 MenuDetail *detail = [[MenuDetail alloc] initWithNibName:@"MenuDetail"      bundle:nil];detail.menu= [menus objectAtIndex:indexPath.row];

[self.navigationController pushViewController:detail animated:YES];
}

-(void)loadMenus
{
NSString* url = @"http://gutech.net63.net/GUeatJson";

Json *myJsonParser = [[Json alloc] init];

[myJsonParser startLoadingObjectWithUrl:url andDelegate:self];

}

-(void)dataRequestCompletedWithJsonObject:(id)jsonObject
{
NSDictionary *menuDictionary = (NSDictionary*)jsonObject; //to check

NSArray* menuArray = (NSArray*)[menuDictionary objectForKey:@"Sun"]; //will see only sun list

self.menus = [[NSMutableArray alloc] init];

for (NSDictionary* dic in menuArray) {

    Menu *menu = [[Menu alloc] init];

    menu.name = [dic objectForKey:@"Name"];//titlle
    menu.image = [dic objectForKey:@"Image"];//yello box url image
    menu.price = [dic objectForKey:@"Price"]; //yellow value

    [menus addObject:menu];
    }

    [self.tableView reloadData];
    }
    @end

When I run it, I will get only the menu of Sunday in my table view. It works fine.

Now I want to use segment control or other tool so that when users choose "sun" they see only Sunday list and so on. How can I do that?

My cell.

- (UITableViewCell *)tableView:(UITableView *)tableView cellForRowAtIndexPath:(NSIndexPath *)indexPath
 {
static NSString *CellIdentifier = @"Cell";

MenuCell *cell = [tableView dequeueReusableCellWithIdentifier:CellIdentifier];
if (cell == nil)
{
    NSArray* objects = [[NSBundle mainBundle] loadNibNamed:@"MenuCell" owner:self   options:nil];

    for(id currentObject in objects)
    {
        if([currentObject isKindOfClass:[UITableViewCell class]])
        {
            cell = ( MenuCell *)currentObject;
            break;
        }
    }

}

Menu *menu = [menus objectAtIndex:indexPath.row];

[cell setDetailsWithMenu:menu];

[cell setSelectionStyle:UITableViewCellSelectionStyleNone];

// Configure the cell...

return cell;

}
rmaddy
  • 314,917
  • 42
  • 532
  • 579
Haitham242
  • 27
  • 7

1 Answers1

0

Create an instance of NSDictionary in your .h-File, call it something like menuDictionary. Change the following line in -(void)dataRequestCompletedWithJsonObject:(id)jsonObject

NSDictionary *menuDictionary = (NSDictionary*)jsonObject; //to check

to

self.menuDictionary = (NSDictionary*)jsonObject; //to check

Add a UISegmentedControl somewhere on your view. Connect the Value Changed-action of your UISegmentedControl to an Action in your viewcontroller, which could look something like this:

- (IBAction)dayChanged:(id)sender {
    UISegmentedControl *segC = (UISegmentedControl*)sender;
    NSArray* menuArray;
    switch(segC.selectedSegmentIndex){
        case 0: //Monday
            menuArray = (NSArray*)[self.menuDictionary objectForKey:@"Mon"];
            break;
        case 1: //Tuesday
            menuArray = (NSArray*)[self.menuDictionary objectForKey:@"Tue"];
            break;
        case 2: //Wednesday
            menuArray = (NSArray*)[self.menuDictionary objectForKey:@"Wed"];
            break;
        case 3: //Thursday
            menuArray = (NSArray*)[self.menuDictionary objectForKey:@"Thu"];
            break;
        case 4: //Friday 
            menuArray = (NSArray*)[self.menuDictionary objectForKey:@"Fri"];
            break;
        case 5: //Saturday
            menuArray = (NSArray*)[self.menuDictionary objectForKey:@"Sat"];
            break;
        case 6: //Sunday
            menuArray = (NSArray*)[self.menuDictionary objectForKey:@"Sun"];
            break;
        default:
            break;
        }

    self.menus = [[NSMutableArray alloc] init];

    for (NSDictionary* dic in menuArray) {

        Menu *menu = [[Menu alloc] init];
        menu.name = [dic objectForKey:@"Name"];//titlle
        menu.image = [dic objectForKey:@"Image"];//yello box url image
        menu.price = [dic objectForKey:@"Price"]; //yellow value

        [menus addObject:menu];
    }
    [self.tableView reloadData];

}

update2:

my UItableview :

#import <UIKit/UIKit.h>
#import "myJson.h"

@interface MenuList : UITableViewController <myJsonDelegate>

@property (nonatomic, retain) NSMutableArray *menus;

@property (nonatomic, retain)NSDictionary *menuDictionary;

- (IBAction)dayChanged:(id)sender;

-(void)loadMenus;
@end
Haitham242
  • 27
  • 7
gasparuff
  • 2,295
  • 29
  • 48
  • okay done..then theUITSegment I create menially first in UTtableView right ? or in view controller.h – Haitham242 Jun 06 '13 at 09:00
  • I create single app at the beginning so it created automatically but am not use it ..so i should add the UITSegment in my UTtableView which show the list right ? and then i implement the code you show me ??? – Haitham242 Jun 06 '13 at 09:10
  • Oky done with UISegment...can u chk the update2 for my UItableview.h? – Haitham242 Jun 06 '13 at 09:26
  • Oh yah yah i connected it work (so happy ) :) thank you very much .. but it to slow to show the result ..I think the better idea ....to have json file for each day serrate ..how can i modify your solution so that i change the json link each time??!!! – Haitham242 Jun 06 '13 at 09:36
  • Oky i will try to search solution for it .another q how can then i have separate the menu according to type breakfast, dinner?? – Haitham242 Jun 06 '13 at 09:44
  • oky one last q do i have to delete ever things in (void)dataRequestCompletedWithJsonObject:(id)jsonObject; except the line you told me to change ? – Haitham242 Jun 06 '13 at 11:17
  • what u mean ? now i only have this line on it self.menuDictionary = (NSDictionary*)jsonObject; //to check coz the other i have in UIsegment ... – Haitham242 Jun 06 '13 at 11:40
  • Oky ... because am trying to figur out why is slow and at the same time will not show the list of first day monday ..until i shows another day and then go back to it .. – Haitham242 Jun 06 '13 at 11:50
  • If you want, you can create a .zip archive of your project, host it somewhere and give me the link. I may have a look on it and help you finding the problem of slow loading. – gasparuff Jun 06 '13 at 11:53
  • If you're looking for personal debugging, Stack Overflow is not the place for it. Please clean up the comments and put clarifications into the answer or the question as appropriate so it can help others. – George Stocker Jun 06 '13 at 12:05
  • I can tell you why it takes so long to load. This is because you are loading the image for every cell from the web. You will need to change this. There are several ways how to do that. One way would be to asynchronously load the images and show some kind of spinner while these images are being loaded, the other way would be to wrap the image of every meal into the json file, as a Base64 encoded string. Then, you will have to decode the Base64 string in your app and show the image. There are also many tutorials for this on the interwebz. Good luck with that ;-) – gasparuff Jun 06 '13 at 12:45
  • am not sure about what u talking abou ... u mean i can use like this website to warp the image http://www.motobit.com/util/base64-decoder-encoder.asp??? – Haitham242 Jun 06 '13 at 12:54
  • Yes, exactly. But it would be better to do it on the server side where the .json file is being generated. If you are using php it's very simple (it won't be rocket science in other programming languages though), but I have used it in PHP in a project some time ago and it worked pretty well. – gasparuff Jun 06 '13 at 13:28
  • no the json i create it manually :( ... so i take the strain Base64 and put there But then i have to get back to its orginal in Xcode..?thank 4 your effort – Haitham242 Jun 06 '13 at 13:40
  • Ok, then just put the Base64 String into the json and then decode it to an UIImage in Xcode. Here's how to do it: http://stackoverflow.com/questions/5890305/how-to-convert-a-base64-encoded-document-into-a-multipage-uiimageview-in-ios – gasparuff Jun 06 '13 at 14:15
  • I Figur that the first day will not show until i switch to other day and then came back, why is that ? – Haitham242 Jun 08 '13 at 07:43
  • This is because you removed almost everything in your `-(void)dataRequestCompletedWithJsonObject:(id)jsonObject`-function. – gasparuff Jun 10 '13 at 07:08