0

我在 Xcode 中创建了一个菜单应用程序。我从JSON 文件中检索了我的菜单。

"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", 
   },]

在我的 json.h 中,我有这行代码,我将在我的 UITableView 中使用:

-(void)dataRequestCompletedWithJsonObject:(id)jsonObject;

然后我在我的 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

当我运行它时,我只会在我的表格视图中获得周日的菜单。它工作正常。

现在我想使用段控制或其他工具,这样当用户选择“太阳”时,他们只会看到星期天列表等等。我怎样才能做到这一点?

我的牢房。

- (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;

}
4

1 回答 1

0

在您的 .h 文件中创建一个 NSDictionary 实例,将其命名为menuDictionary. 更改以下行-(void)dataRequestCompletedWithJsonObject:(id)jsonObject

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

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

在视图的某处添加 UISegmentedControl。将 UISegmentedControl的 -action 连接Value Changed到 viewcontroller 中的 Action,如下所示:

- (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];

}

更新2:

我的 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
于 2013-06-06T08:46:47.570 回答