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