xcode 在“UITableView”中选择一行时调用新视图

声明:本页面是StackOverFlow热门问题的中英对照翻译,遵循CC BY-SA 4.0协议,如果您需要使用它,必须同样遵循CC BY-SA许可,注明原文地址和作者信息,同时你必须将它归于原作者(不是我):StackOverFlow 原文地址: http://stackoverflow.com/questions/5724311/
Warning: these are provided under cc-by-sa 4.0 license. You are free to use/share it, But you must attribute it to the original authors (not me): StackOverFlow

提示:将鼠标放在中文语句上可以显示对应的英文。显示中英文
时间:2020-09-14 20:51:36  来源:igfitidea点击:

Calling a New View when selecting a Row in a 'UITableView'

iphoneobjective-cxcodeiosuitableview

提问by Paul Morris

I am currently writing my first iPhone app, but have encountered an issue. I have a view which contains a UITableView. This is the first time that I have attempted this, and this is the behaviour that I am trying to achieve:

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

When the user selects one of the rows, I would like this to call a new view, taking the user to a different page displaying info in reference to what they have selected.

当用户选择其中一行时,我希望它调用一个新视图,将用户带到显示与他们选择的内容相关的信息的不同页面。

I have it currently, so when the user selects a row it displays a UIAlert in the same view, but this doesn;t suit my needs. I have set the UITableView up through interface builder, and inputted the following code into my .m file to set it up.

我目前拥有它,所以当用户选择一行时,它会在同一视图中显示一个 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;
}

This creates a list of ten rows. The following codes gives me my UIAlert when tapped, however, I want to remove this and make it call a new view of my choice;

这将创建一个包含十行的列表。以下代码在点击时为我提供了 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];   

}

Can anyone help with this last piece of code? the view I want it to call is called 'ProteinView'.

任何人都可以帮助完成最后一段代码吗?我希望它调用的视图称为“ProteinView”。

采纳答案by Sebastien Peek

Alrighty, what we need to do is use one of the UITableView methods that are already readily available to us. We'll do the following.

好的,我们需要做的是使用我们已经很容易使用的 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];
}

EDIT

编辑

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

I haven't compiled this, so I don't know if it'll fully work. But that is definitely the fastest and most easiest way to do it!

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

回答by Hubert Kunnemeyer

You could present the view modally like this

您可以像这样以模态方式呈现视图

YourViewController2 *viewController2 = [[YourViewController2 alloc]initWithNibName:@"YourViewController2" bundle:nil];
[self presentModalViewController:viewController2 animated:YES];

Do you have more than one view to present? If so you will need to create an array with the names, pass it into the tableview and then present the correct view for the row selected based on the indexPath.row.

您是否有不止一种观点要呈现?如果是这样,您将需要创建一个带有名称的数组,将其传递到 tableview,然后根据 indexPath.row 为所选行显示正确的视图。

回答by d4ndym1k3

you'll have to open your MainWindow.xib and add a navigation controller to it. Then add a navigation controller outlet to your app delegate and connect them. Then you'll need to set the navigation controller's view as your main window's view.

您必须打开 MainWindow.xib 并向其添加导航控制器。然后将导航控制器插座添加到您的应用程序委托并连接它们。然后您需要将导航控制器的视图设置为主窗口的视图。

You can add a table view to any iPhone app fairly easily, just by creating a new UITableViewController subclass from the File -> New command.

你可以很容易地向任何 iPhone 应用程序添加表格视图,只需从 File -> New 命令创建一个新的 UITableViewController 子类。

Even if you go this route, I would suggest creating a new navigation-based project to use as a template/cheat-sheet.

即使你走这条路,我还是建议创建一个新的基于导航的项目来用作模板/备忘单。