【发布时间】:2011-04-20 00:30:11
【问题描述】:
我目前正在编写我的第一个 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;
}
这将创建一个包含十行的列表。以下代码在点击时为我提供了 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];
}
任何人都可以帮助完成最后一段代码吗?我希望它调用的视图称为“ProteinView”。
【问题讨论】:
标签: iphone objective-c xcode ios uitableview