UITegment控件在UITableView-Json数据中

时间:2013-06-06 08:25:41

标签: json uitableview uisegmentedcontrol

我在Xcode中创建了一个菜单应用程序。我从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", 
   },]

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

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

现在我想使用段控制或其他工具,这样当用户选择“sun”时,他们只能看到星期日列表,依此类推。我怎么能这样做?

我的手机。

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

}

1 个答案:

答案 0 :(得分:0)

在.h-File中创建一个NSDictionary实例,称之为menuDictionary。 更改-(void)dataRequestCompletedWithJsonObject:(id)jsonObject

中的以下行
NSDictionary *menuDictionary = (NSDictionary*)jsonObject; //to check

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

在视图的某处添加UISegmentedControl。将您的UISegmentedControl的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];

}

<强> UPDATE2:

我的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