-1

In my iphone design, there are two table views added.

One table view is for displaying the MenuList for hotels using NsDictionary(Like Veg. NonVeg, Beverages..) and the other table view is for displaying the ListItems(Like In veg, i will Have Paneer,Rotti..) . When ever a menuList cell is selected the files inside the selected list needs to to be displayed in the other table view(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 did select row

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



}







}

second table :

// Is this right to declare the lists here same as my first table. Please help me in this one.

@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;
}
  • Please post your code.. – HRM Feb 03 '14 at 09:32
  • i can't able to post :( oops can someone help pls – Faisal Ahamed Feb 03 '14 at 10:15
  • You need to edit and add the code. Dont add code in comment. – HRM Feb 03 '14 at 10:18
  • it shows this error .! if you don't mind. can i email you ? your post appears to contain code that is not properly formatted as code. Please indent all code by 4 spaces using the code toolbar button or the CTRL+K keyboard shortcut. For more editing help, click the [?] toolbar icon. – Faisal Ahamed Feb 03 '14 at 10:54
  • @HRM here is my code added..please help. – Faisal Ahamed Feb 04 '14 at 08:41
  • @HRM can you see my code ? give me a solution please – Faisal Ahamed Feb 04 '14 at 11:06
  • You should implement didSelectRowAtIndexPath and then push your DetailTableViewController from there. – HRM Feb 04 '14 at 11:37
  • so should i delete Segue method ? – Faisal Ahamed Feb 04 '14 at 11:47
  • sir, just check my description now. i have added my second table and some changes. please help me to to this. i am confused a lot to do this. I will be very thankful to you if you solved me this problem. Help – Faisal Ahamed Feb 04 '14 at 11:59
  • help pls. sir still i can't able to get correct output. can you edit my code correct it please. – Faisal Ahamed Feb 13 '14 at 06:14
  • @HRM sir, i have an updated code for this same one. can you help me out for output ? – Faisal Ahamed Feb 18 '14 at 09:58
  • detailVC.detailData = dictToPass; You already passing the data to detailController. Just use that dictionary. Dont create new one. – HRM Feb 18 '14 at 10:08
  • its not shown anything in second table. its empty. i can't able to figure it out.! i have one new code. can i post it here ? – Faisal Ahamed Feb 18 '14 at 10:23
  • In detailtablecontroller's cellForRowAtIndexPath, use detailData dictionary instead of vegDict – HRM Feb 18 '14 at 10:25
  • its working.. but in second table i can see only"rotti, panneer and chappathi " .whatever the item i clicked. i mean it displays same item when i click veg,nonveg or beverages – Faisal Ahamed Feb 18 '14 at 10:54
  • NSLog the detailData in detailTableViewController. Did you pass the correct values in didSelectRow? – HRM Feb 18 '14 at 11:04
  • @HRM i uncommented these lines in didSelectRow "if([key isEqualToString:@"1"] ){ dictToPass = [[NSDictionary alloc]initWithObjectsAndKeys:@"1",@"Rotti" , @"2", @"Panner", nil] ; } " but its still working by detail view because i declared veg values there. should i create separate file nonveg and beverages ? – Faisal Ahamed Feb 18 '14 at 11:48
  • No you dont need to create separate files for that. You need to remove veg or nonveg related things from detailTableViewcontroller and just pass the selected items from your first tableviewcontroller. If still doesn't work, I recommend you to check some example codes of navigating viewcontrollers, passing data between controllers and then you will get a clear idea. – HRM Feb 18 '14 at 12:04
  • @HRM ok sir. let me try . thank you . i understand something now . if you know some good tutorials for this. just refer me . thanks :) – Faisal Ahamed Feb 18 '14 at 12:06

1 Answers1

0

I guess you want your vegDict property in DetailTableViewController to be filled based on user selection in first tableview.

Currently what you are doing is from prepareForSegue method (I dont know how this been called as you haven't mentioned about creating any seques in between your first and second controller), set the vegDict property some values and same values are again set on its viewDidLoad method. That is not required.

The thing you should do is to implement didSelectRowAtIndexPath, then get the tapped row's content's dictionary and then set the detailTableViewController.vegDict to that. Push the detailTableViewController from here.

UPDATE based on comments:

Your DetailTableViewController should have a property say detailData for holding the data that you wish to display in detailTableViewController. Now you need to assign this detailData based on the menu selection. For example in your case, if user selects veg, then you should do the following in 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];
Cilan
  • 13,101
  • 3
  • 34
  • 51
HRM
  • 2,097
  • 6
  • 23
  • 37
  • Sir. where should i add my vegDict ? in my secondtable or firsttable. I did this in didSelectRowAtIndexPath.{ DetailTableViewController *dvc = [[DetailTableViewController alloc]init] ; dvc.vegDIct ; } but in output still i cant see anything in secondtable. – Faisal Ahamed Feb 04 '14 at 12:32
  • can you please give me sample code for this one. I want to Understand. If I click Veg Item in my first Table then i want Veglist(such as Paner,rotti) on my second table using nsdictionary. Thats my aim now. – Faisal Ahamed Feb 04 '14 at 12:46
  • Please check this to get an understanding on how to pass data between controllers. http://stackoverflow.com/questions/5210535/passing-data-between-view-controllers – HRM Feb 05 '14 at 04:45
  • Also, pls check your numberOfRowsInSection in DetailsController as its returning 1 only. – HRM Feb 05 '14 at 04:46
  • Its Useful sir. i am returning one only. now my Doubt is where to declare the detail table list and how ? using NSDictionary. I am confused. Still i cant able to see my expected output, – Faisal Ahamed Feb 05 '14 at 05:24
  • can you please say now how to add the detail list ? – Faisal Ahamed Feb 05 '14 at 05:42
  • now i understand whats happening by the updated comment.. detailData property should be a string or dictionary sir ? – Faisal Ahamed Feb 05 '14 at 06:03
  • sit. aim getting this message "nested push animation can result in corrupted navigation bar 2014-02-05 11:53:35.316 list[1252:70b] Finishing up a navigation transition in an unexpected state. Navigation Bar subview tree might get corrupted. " i declared detail data proper as nsdictionary and i don't have anything on viewdidload in detail table. Help Please – Faisal Ahamed Feb 05 '14 at 06:24
  • and also how it will know that am using this detail for Veg. If i want another one detail list for NonVeg, can i use another DIdSelectRowAtIndexpath Like the same ? Pls Help. at least i need to complete this soon. so please – Faisal Ahamed Feb 05 '14 at 06:32
  • please Help me to overcome this situation. I am so Confused by this – Faisal Ahamed Feb 05 '14 at 06:59
  • Again updated. If still not working, then edit and post your current working code of didSelectRow. – HRM Feb 05 '14 at 07:13
  • i have updated my did selectrow above. still it shows the same runtime error as said above. and also sir, is it necessary to change anything in detail table viewdidload ? – Faisal Ahamed Feb 05 '14 at 11:18
  • sir. can you please correct my error in this and give me a proper code for this using NSDictionary, please Help ! – Faisal Ahamed Feb 08 '14 at 05:38
  • check out http://stackoverflow.com/questions/11813091/nested-push-animation-can-result-in-corrupted-navigation-bar-multiple-warning. – HRM Feb 10 '14 at 04:59
  • help pls. sir still i can't able to get correct output. can you edit my code correct it please..in this one – Faisal Ahamed Feb 13 '14 at 06:18