【发布时间】:2023-03-18 08:32:01
【问题描述】:
在我的 iphone 设计中,添加了两个表格视图。
一个表格视图用于显示使用 NsDictionary(Like Veg. NonVeg, Beverages..) 的酒店的 MenuList,另一个表格视图用于显示 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;
}
//更新确实选择了行
-(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;
}
【问题讨论】:
-
请发布您的代码..
-
我无法发帖:(哎呀,有人可以帮忙
-
您需要编辑和添加代码。不要在注释中添加代码。
-
它显示了这个错误。!如果你不介意。我可以给你发电子邮件吗?您的帖子似乎包含未正确格式化为代码的代码。请使用代码工具栏按钮或 CTRL+K 键盘快捷键将所有代码缩进 4 个空格。如需更多编辑帮助,请单击 [?] 工具栏图标。
-
@HRM 这是我添加的代码..请帮助。
标签: xcode5