在“UITableView”中select行时调用新视图

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

当用户select其中一个行时,我希望这个调用一个新的视图,把用户带到不同的页面,显示他们所select的信息。

我现在有,所以当用户select一行时,它会在同一个视图中显示一个UIAlert,但是这不适合我的需求。 我已经通过接口生成器设置了UITableView,并将下面的代码input到我的.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; } 

这将创build十行的列表。 下面的代码给我我的UIAlert,当我点击,但是,我想删除这个,并呼吁一个新的观点,我的select;

 - (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]; } 

任何人都可以帮助这最后一块代码? 我想要它调用的视图叫做“ProteinView”。

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

编辑

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

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

你可以像这样模态地呈现视图

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

你有不止一种观点来呈现? 如果是这样,你将需要创build一个名称的数组,将其传递到tableview,然后呈现基于indexPath.row选定的行的正确视图。

你将不得不打开你的MainWindow.xib并添加一个导航控制器。 然后将导航控制器sockets添加到您的应用程序委托并连接它们。 然后,您需要将导航控制器的视图设置为主窗口的视图。

您可以相当容易地将表格视图添加到任何iPhone应用程序,只需从File – > New命令中创build一个新的UITableViewController子类即可。

即使你走这条路,我也会build议创build一个新的基于导航的项目作为模板/作弊表。 UITableView – 创build一个简单的表视图