如何使用NSMutableDictionary连接Tableview和View Controller?

时间:2014-02-03 09:12:31

标签: xcode5

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

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

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

}

// updated确实选择了行

 -(void)tableView:(UITableView *)tableView didSelectRowAtIndexPath:(NSIndexPath *)indexPath
{
    NSString *key = [self.menuDict allKeys][indexPath.row];
    NSDictionary *dictToPass = nil;
    if([key isEqualToString:@"1"] ){
        dictToPass = [[NSDictionary alloc]initWithObjectsAndKeys:@"1",@"Rotti" , @"2", @"Panner",  nil] ;
    }
    else{
        if([key isEqualToString:@"2"] ){
            dictToPass = [[NSDictionary alloc]initWithObjectsAndKeys:@"1",@"chicken" , @"2", @"mutton",  nil] ;
        }

    }

    DetailTableViewController *detailVC = [[DetailTableViewController alloc]init];
    detailVC.detailData = dictToPass;
    [self.navigationController pushViewController:detailVC animated:YES];



}







}

第二张表:

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

@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)

我想您希望根据第一个表格视图中的用户选择来填充vegDict中的DetailTableViewController属性。

目前您所做的是来自prepareForSegue方法(我不知道如何调用此方法,因为您没有提到在第一个和第二个控制器之间创建任何seques),设置vegDict属性在其viewDidLoad方法上再次设置一些值和相同的值。这不是必需的。

你应该做的是实现didSelectRowAtIndexPath,然后获取被点击的行的内容的字典,然后将detailTableViewController.vegDict设置为该字典。从此处推送detailTableViewController

根据评论更新:

您的DetailTableViewController应该有一个属性detailData,用于保存您希望在detailTableViewController中显示的数据。现在,您需要根据菜单选择分配此detailData。例如,在您的情况下,如果用户选择了veg,那么您应该在didSelectRowAtIndexPath

中执行以下操作
NSString *key = [self.menuDict allKeys][indexPath.row];
NSDictionary *dictToPass = nil;
if([key isEqualToString:@"1"] ){
   dictToPass = [[NSDictionary alloc]initWithObjectsAndKeys:@"1",@"Rotti" , @"2", @"Panner",  nil] ;
}
else{
   //Create for non-veg
}

DetailTableViewController *detailVC = [[DetailTableViewController alloc]init];
detailVC.detailData = dictToPass;
[self.navigationController pushViewController:detailVC animated:YES];