【问题标题】:How to Connect Tableview and View Controller Using NSMutableDictionary?如何使用 NSMutableDictionary 连接 Tableview 和 View Controller?
【发布时间】: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


【解决方案1】:

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

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

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

基于 cmets 的更新:

您的 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];

【讨论】:

  • 先生。我应该在哪里添加我的 vegDict ?在我的第二张桌子或第一张桌子上。我在 didSelectRowAtIndexPath.{ DetailTableViewController *dvc = [[DetailTableViewController alloc]init] 中做到了这一点dvc.vegDIct ; } 但在输出中我仍然看不到 secondtable 中的任何内容。
  • 你能给我这个示例代码吗?我想了解。如果我在我的第一个表中单击 Veg 项目,那么我希望使用 nsdictionary 在我的第二个表上使用 Veglist(例如 Paner、rotti)。这就是我现在的目标。
  • 请检查此以了解如何在控制器之间传递数据。 stackoverflow.com/questions/5210535/…
  • 另外,请检查您在 DetailsController 中的 numberOfRowsInSection 是否仅返回 1。
  • 它很有用,先生。我只返回一个。现在我的疑问是在哪里声明明细表列表以及如何声明?使用 NSDictionary。我很困惑。我仍然无法看到我的预期输出,
猜你喜欢
  • 1970-01-01
  • 1970-01-01
  • 2020-04-18
  • 2013-03-24
  • 1970-01-01
  • 2012-07-17
  • 1970-01-01
  • 1970-01-01
  • 1970-01-01
相关资源
最近更新 更多