在"UITableView"中选择行时调用新视图

Pau*_*ris 7 iphone xcode objective-c uitableview ios

我目前正在编写我的第一个iPhone应用程序,但遇到了一个问题.我有一个包含UITableView的视图.这是我第一次尝试这个,这是我想要实现的行为:

当用户选择其中一行时,我希望这可以调用一个新视图,将用户带到显示参考所选内容的不同页面.

我现在有它,所以当用户选择一行时,它会在同一视图中显示UIAlert,但这并不适合我的需要.我已通过界面构建​​器设置了UITableView,并将以下代码输入到我的.m文件中进行设置.

- (NSInteger)tableView:(UITableView *)tableView 
 numberOfRowsInSection:(NSInteger)section {
    //return the value
    return 10;
}

//now we define the cells.
- (UITableViewCell *)tableView:(UITableView *)tableView 
         cellForRowAtIndexPath:(NSIndexPath *)indexPath {

    // Identifier for retrieving reusable cells.
    static NSString *cellIdentifier = @"MyCellIdentifier";

    // Attempt to request the reusable cell.
    UITableViewCell *cell = [tableView dequeueReusableCellWithIdentifier:cellIdentifier];

    // No cell available - create one
    if(cell == nil) {
        cell = [[UITableViewCell alloc] initWithStyle:UITableViewCellStyleDefault 
                                      reuseIdentifier:cellIdentifier];
    }

    // Set the text of the cell to the row index.
    cell.textLabel.text = [NSString stringWithFormat:@"iPad %d", indexPath.row];

    return cell;
}
Run Code Online (Sandbox Code Playgroud)

这将创建一个包含十行的列表.以下代码在点击时给了我我的UIAlert,但是,我想删除它并让它调用我选择的新视图;

- (void)tableView:(UITableView *)tableView 
didSelectRowAtIndexPath:(NSIndexPath *)indexPath {

    // Show an alert with the index selected.
    UIAlertView *alert = [[UIAlertView alloc] 
                          initWithTitle:@"iPad Selected"                         
                          message:[NSString stringWithFormat:@"iPad %d", indexPath.row]                     
                          delegate:self       
                          cancelButtonTitle:@"OK"           
                          otherButtonTitles:nil];
    [alert show];
    [alert release];   

}
Run Code Online (Sandbox Code Playgroud)

最后一段代码可以帮助任何人吗?我希望它调用的视图叫做'ProteinView'.

Seb*_*eek 3

好吧,我们需要做的是使用我们已经可以使用的 UITableView 方法之一。我们将进行以下操作。

- (void)tableView:(UITableView *)tableView 
didSelectRowAtIndexPath:(NSIndexPath *)indexPath {

ProteinView *detailViewController = [[ProteinView alloc] initWithNibName:@"ProteinView" bundle:nil];

        // It is here we'd pass information from the currently selected UITableViewCell to the ProteinView.
        // An example of this is the following.

        // I would do it like this, but others would differ slightly.
        NSString *titleString = [[[NSString alloc] initWithFormat:@"iPad %d",indexPath.row] autorelease];

        // title is an object of detailViewController (ProteinView). In my own instances, I have always made a NSString which in viewDiDLoad is made the self.navigationBar.title string. Look below for what my ProteinView.m and .h would look like.
        detailViewController.stringTitle = titleString;
        // ...
        // Pass the selected object to the new view controller.
        [self.navigationController pushViewController:detailViewController animated:YES];
        [detailViewController release];
}
Run Code Online (Sandbox Code Playgroud)

编辑

// -------- ProteinView.m -------- //

- (void)viewDidLoad {

[super viewDidLoad];
// Do any additional setup after loading the view from its nib.

// Here we set the navigationItem.title to the stringTitle. stringTitle is declared in the .h. Think of it as a global scope variable. It is also propertised in the .h and then synthesized in the .m of ProteinView. 
self.navigationItem.title = stringTitle;
}
Run Code Online (Sandbox Code Playgroud)

我还没有编译这个,所以我不知道它是否能完全工作。但这绝对是最快、最简单的方法!