【问题标题】:Information on pushing to new DetailView [duplicate]有关推送到新 DetailView 的信息 [重复]
【发布时间】:2014-04-05 20:20:12
【问题描述】:

有谁知道那里有任何教程解释如何推送到新的详细信息视图。现在我有 2 个 TableView,第一个 TableView 保存状态,第二个 TableView 保存区域名称。从第二个 TableView 我希望能够按下一个区域并在 DetailView 中显示信息,但我不确定如何将该信息获取到 DetailView。

RootTableViewController.h

#import <UIKit/UIKit.h>

@interface RootTableViewController : UITableViewController

@end

RootTableViewController.m

#import "RootTableViewController.h"
#import "SecondTableViewController.h"

@interface RootTableViewController ()

@end

@implementation RootTableViewController
{
NSMutableArray *states;
}

- (id)initWithStyle:(UITableViewStyle)style
{
self = [super initWithStyle:style];
if (self) {
    // Custom initialization
}
return self;
}

- (void)viewDidLoad
{
[super viewDidLoad];

states = [NSMutableArray arrayWithObjects:@"Alabama", @"Georgia", @"Tennessee", nil];

// Uncomment the following line to preserve selection between presentations.
// self.clearsSelectionOnViewWillAppear = NO;

// Uncomment the following line to display an Edit button in the navigation bar for this view controller.
// self.navigationItem.rightBarButtonItem = self.editButtonItem;
}

- (void)didReceiveMemoryWarning
{
[super didReceiveMemoryWarning];
// Dispose of any resources that can be recreated.
}

#pragma mark - Table view data source

- (NSInteger)numberOfSectionsInTableView:(UITableView *)tableView
{
return 1;
}

- (NSInteger)tableView:(UITableView *)tableView numberOfRowsInSection:(NSInteger)section
{
return [states count];
}


- (UITableViewCell *)tableView:(UITableView *)tableView cellForRowAtIndexPath:(NSIndexPath *)indexPath
{
static NSString *simpleTableIdentifier = @"StatesCell";
UITableViewCell *cell = [tableView dequeueReusableCellWithIdentifier:simpleTableIdentifier];

if(cell == nil)
{
    cell = [[UITableViewCell alloc] initWithStyle:UITableViewCellStyleDefault reuseIdentifier:simpleTableIdentifier];
}

cell.textLabel.text = [states objectAtIndex:indexPath.row];
return cell;
}

SecondTableViewController.h

#import <UIKit/UIKit.h>

@interface SecondTableViewController : UITableViewController
{
NSMutableArray *listOfStates;
}
@property (retain, atomic) NSMutableArray *listOfStates;
@property (nonatomic, strong) NSString *stateName;

@end

SecondTableViewController.m

#import "SecondTableViewController.h"
#import "RootTableViewController.h"

@interface SecondTableViewController ()

@end

@implementation SecondTableViewController

@synthesize listOfStates = _listOfStates;

- (id)initWithStyle:(UITableViewStyle)style
{
self = [super initWithStyle:style];
if (self) {
    // Custom initialization
}
return self;
}

- (void)viewDidLoad
{
[super viewDidLoad];

_listOfStates = [NSDictionary dictionary];


_listOfStates  =
@{
@"Alabama":@[
        @{@"area":@"albama area 1", @"description":@"Alabama specs 1"},
        @{@"area":@"albama area 2", @"description":@"Alabama specs 2"},
        @{@"area":@"albama area 3", @"description":@"Alabama specs 3"},
        ],
@"Georgia":@[
        @{@"area":@"Georgia area 1", @"description":@"Georgia specs 1"},
        @{@"area":@"Georgia area 2", @"description":@"Georgia specs 2"},
        @{@"area":@"Georgia area 3", @"description":@"Georgia specs 3"}
        ],
@"Tennessee":@[
        @{@"area":@"Tennessee area 1", @"description":@"Tennessee specs 1"},
        @{@"area":@"Tennessee area 2", @"description":@"Tennessee specs 2"},
        @{@"area":@"Tennessee area 3", @"description":@"Tennessee specs 3"}
        ]

};

NSArray *state = [_listOfStates objectForKey:_stateName];

// Uncomment the following line to preserve selection between presentations.
// self.clearsSelectionOnViewWillAppear = NO;

// Uncomment the following line to display an Edit button in the navigation bar for this view controller.
// self.navigationItem.rightBarButtonItem = self.editButtonItem;

- (void)didReceiveMemoryWarning
{
[super didReceiveMemoryWarning];
// Dispose of any resources that can be recreated.
}

#pragma mark - Table view data source

- (NSInteger)numberOfSectionsInTableView:(UITableView *)tableView
{
return 1;
}

- (NSInteger)tableView:(UITableView *)tableView numberOfRowsInSection:(NSInteger)section
{
return [self.state count];
}


- (UITableViewCell *)tableView:(UITableView *)tableView cellForRowAtIndexPath:(NSIndexPath    *)indexPath
{
static NSString *simpleTableIdentifier = @"Animal2Cell";

UITableViewCell *cell = [tableView dequeueReusableCellWithIdentifier:simpleTableIdentifier];
cell.textLabel.text = [[self.state objectAtIndex:indexPath.row] objectForkey:@"area"];

return cell;
}

-(void)tableView:(UITableView *)tableView didSelectRowAtIndexPath:(NSIndexPath *)indexPath
{
[self performSegueWithIdentifier:@"detailSegue" sender:sender];
}

#pragma mark - Navigation
- (void)prepareForSegue:(UIStoryboardSegue *)segue sender:(id)sender
{
// Make sure your segue name in storyboard is the same as this line
if ([[segue identifier] isEqualToString:@"detailSegue"])
{
    //get the specs
    NSIndexPath *indexPath = [self.tableView indexPathForSelectedRow];
    NSString *specs = [_informationSpecs objectAtIndex:indexPath.row];

    //set the specs
    DetailViewController *detailVC= (DetailViewController *)segue.destinationViewController;
    detailVC.description= [[self.state objectAtIndex:indexPath.row] objectForkey:@"description"];
}
}

@end

错误

SecondTableViewController.m:43:10: Expected ':'

SecondTableViewController.m:58:37: No visible @interface for 'NSDictionary' declares the selector 'obejectForKey:'

SecondTableViewController.m:67:9: Use of undeclared identifier 'didReceiveMemoryWarning'

感谢您的指导。

【问题讨论】:

标签: ios objective-c uitableview detailsview


【解决方案1】:

您应该将数据放在一起,而不是为每个状态设置数组,因为它们是相关的。

国家列表:

_listOfStates  =
@{
  @"Alabama":@[
            @{@"area":@"albama area 1", @"description":@"Alabama specs 1"},
            @{@"area":@"albama area 2", @"description":@"Alabama specs 2"},
            @{@"area":@"albama area 3", @"description":@"Alabama specs 3"},
            ],
  @"Georgia":@[
            @{@"area":@"Georgia area 1", @"description":@"Georgia specs 1"},
            @{@"area":@"Georgia area 2", @"description":@"Georgia specs 2"},
            @{@"area":@"Georgia area 3", @"description":@"Georgia specs 3"}
            ],
  @"Tennessee":@[
            @{@"area":@"Tennessee area 1", @"description":@"Tennessee specs 1"},
            @{@"area":@"Tennessee area 2", @"description":@"Tennessee specs 2"},
            @{@"area":@"Tennessee area 3", @"description":@"Tennessee specs 3"}
            ]

  };

状态数组:

self.state = [_listOfStates objectForKey:_stateName];

numberOfRowsInSection

- (NSInteger)tableView:(UITableView *)tableView numberOfRowsInSection:(NSInteger)section
{
    return [self.state count];
}

cellForRowAtIndexPath

- (UITableViewCell *)tableView:(UITableView *)tableView cellForRowAtIndexPath:(NSIndexPath *)indexPath
{
    static NSString *simpleTableIdentifier = @"Animal2Cell";

    UITableViewCell *cell = [tableView dequeueReusableCellWithIdentifier:simpleTableIdentifier];
    cell.textLabel.text = [[self.state objectAtIndex:indexPath.row] objectForkey:@"area"];

    return cell;
}

didSelectRowAtIndexPath

 -(void)tableView:(UITableView *)tableView 
                            didSelectRowAtIndexPath:(NSIndexPath *)indexPath{
   [self performSegueWithIdentifier:@"detailSegue" sender:sender];
 }

prepareForSegue

 - (void)prepareForSegue:(UIStoryboardSegue *)segue sender:(id)sender
{
    // Make sure your segue name in storyboard is the same as this line
    if ([[segue identifier] isEqualToString:@"detailSegue"])
    {  
       //get the specs
       NSIndexPath *indexPath = [self.tableView indexPathForSelectedRow];
       NSString *description = [[self.state objectAtIndex:indexPath.row] objectForkey:@"description"];

       //set the specs
       DetailViewController *detailVC= (DetailViewController *)segue.destinationViewController;
       detailVC.description= description;
    }
}

DEMO PROJECT

【讨论】:

  • 我最困惑的是在我的程序中在哪里添加区域规格?在什么方法下以及如何改变我的“cellForRowAtIndexPath”、“numberOfRowsInSection”和“numberOfSectionsInTableView”,因为我将添加更多数组......
  • 您需要将数据关联在一起,这就是您的问题,例如 _listOfSpecs = @[ @{@"name" :@"Alabama", @"specs":@"Alabama specs"}, @{@"name" :@"Georgia", @"value":@"Georgia specs"}, @{@"name" :@"Tennessee", @"value":@"Tennessee specs"}, ];
  • 您可以尝试实施上述答案吗?我很难遵循这一点。对不起,我真的很陌生,我知道这可能很简单。
  • 有哪些小问题?为什么在声明时说它们未声明?
  • @javaGeek soory 这是一个错字objectForKey 第一个错误和最后一个错误的行在哪里
【解决方案2】:

在您的详细视图中实现一个方法,该方法接收更改的数据并相应地更新局部变量(如果有)并更新视图。 从您的主视图控制器调用该方法。

【讨论】:

  • 如果我发布了我的代码,您能否让我朝着正确的方向开始?
猜你喜欢
  • 2016-10-13
  • 2013-08-07
  • 1970-01-01
  • 2011-07-04
  • 1970-01-01
  • 1970-01-01
  • 1970-01-01
  • 1970-01-01
  • 1970-01-01
相关资源
最近更新 更多