如何使用NSDictionary连接两个表?

时间:2014-02-04 06:28:17

标签: nsdictionary nsmutabledictionary

在我的iphone设计中,添加了两个表格视图。

一个表视图用于显示使用NsDictionary(如Veg.NonVeg,Beverages ..)的酒店的MenuList,另一个表视图用于显示ListItems(如In veg,i will Have Paneer,Rotti ..)。当选择了menuList单元格时,所选列表中的文件需要显示在另一个表格视图中(ListIems)。

我的问题是

我很担心

我想在这两个表中使用NsDictionary Alone。任何人都可以给我一个简单的代码。我很困惑,而且更新鲜。如果有人教我,请提前谢谢。请帮忙。我使用xcode5

@interface TableViewController ()

@end

@implementation TableViewController
@synthesize menuDict ;


- (id)initWithStyle:(UITableViewStyle)style
{
    self = [super initWithStyle:style];
    if (self) {
        // Custom initialization
    }
    return self;
}

- (void)viewDidLoad
{
        [super viewDidLoad];

    menuName = [NSArray arrayWithObjects:@"Veg", @"NonVeg", @"Beverages", nil];
    menuId = [NSArray arrayWithObjects:@"1", @"2", @"3", nil];

    self.menuDict = [NSDictionary dictionaryWithObjects:menuName
                                                     forKeys:menuId];


}

- (void)didReceiveMemoryWarning
{
    [super didReceiveMemoryWarning];
    // Dispose of any resources that can be recreated.
}

#pragma mark - Table view data source

- (NSInteger)numberOfSectionsInTableView:(UITableView *)tableView
{
    // Return the number of sections.
    return 1;
}

-(NSInteger)tableView:(UITableView *)tableView numberOfRowsInSection:(NSInteger)section
{
    return [[self.menuDict allKeys] count];
}


-(UITableViewCell *)tableView:(UITableView *)tableView cellForRowAtIndexPath:(NSIndexPath *)indexPath
{



    static NSString *unifiedID = @"TableCell";
    UITableViewCell *cell = [tableView dequeueReusableCellWithIdentifier:unifiedID];
    if (!cell) {
        cell = [[UITableViewCell alloc] initWithStyle:UITableViewCellStyleDefault reuseIdentifier:unifiedID];

    }

    for (id key in self.menuDict) {
        NSLog(@"key: %@, value: %@", key, [self.menuDict objectForKey:key]);
    }

    NSString *key = [self.menuDict allKeys][indexPath.row];

    NSString *menuNameString = self.menuDict[key];
    NSString *menuIdString = key;
    cell.textLabel.text  = menuNameString;
    cell.detailTextLabel.text  = menuIdString;

    return cell;

}






- (void)prepareForSegue:(UIStoryboardSegue *)segue sender:(id)sender
{

    if([[segue identifier] isEqualToString:@"ShowDetails"])
    {
           NSDictionary *vvegDict = [[NSDictionary alloc]initWithObjectsAndKeys:@"1",@"Rotti" , @"2", @"Panner",  nil] ;
        DetailTableViewController *tvc = segue.destinationViewController ;
    tvc.vegDict =vvegDict ;




    }


}

第二张表:

//这是否正确声明列表与我的第一个表相同。请帮助我。

@interface DetailTableViewController ()

@end

@implementation DetailTableViewController
@synthesize vegName,vegId ;



- (id)initWithNibName:(NSString *)nibNameOrNil bundle:(NSBundle *)nibBundleOrNil
{
    self = [super initWithNibName:nibNameOrNil bundle:nibBundleOrNil];
    if (self) {
        // Custom initialization
    }
    return self;
}

- (void)viewDidLoad
{
    [super viewDidLoad];

    vegName = [NSArray arrayWithObjects:@"Rotti", @"Panneer", @"Chappathi", nil];
    vegId = [NSArray arrayWithObjects:@"1", @"2", @"3", nil];

    self.vegDict = [NSDictionary dictionaryWithObjects:vegName
                                                forKeys:vegId];

    // Do any additional setup after loading the view.
}

- (void)didReceiveMemoryWarning
{
    [super didReceiveMemoryWarning];
    // Dispose of any resources that can be recreated.
}

-(NSInteger)numberOfSectionsInTableView:(UITableView *)tableView
{
    return  1 ;
}

-(NSInteger)tableView:(UITableView *)tableView numberOfRowsInSection:(NSInteger)section
{
    return [[self.vegDict allKeys]count] ;
    return 1 ;
}
-(UITableViewCell *)tableView:(UITableView *)tableView cellForRowAtIndexPath:(NSIndexPath *)indexPath
{

    static NSString *unifiedID = @"aCellID";
    UITableViewCell *cell = [tableView dequeueReusableCellWithIdentifier:unifiedID];
    if (!cell) {
        cell = [[UITableViewCell alloc] initWithStyle:UITableViewCellStyleDefault reuseIdentifier:unifiedID];

    }

    for (id key in self.vegDict) {
        NSLog(@"key: %@, value: %@", key, [self.vegDict objectForKey:key]);
    }

    NSString *key = [self.vegDict allKeys][indexPath.row];

    NSString *vegNameString = self.vegDict[key];
    NSString *vegIdString = key;
    cell.textLabel.text  = vegNameString;
    cell.detailTextLabel.text  = vegIdString;

    return cell;
}

1 个答案:

答案 0 :(得分:0)

如果您使用Segues从一个uitableviewcontroller转到另一个,那么您可以使用

-prepareForSegue:sender:

在此方法中,您可以在目标视图控制器中设置NSDictionary属性。您可以从sender参数获取目标视图控制器。