我对 UITableViewController 很陌生,我想制作一个静态单元格 UITableViewController,每个静态单元格打开相同的 nib 文件,但 UIWebView 的 URL 会不同。例如第 1 行将打开 google.com,第 2 行将打开 yahoo.com。我可以知道我该怎么做吗?
谢谢
最佳答案
您需要实现 tableview 委托(delegate)方法 tableView:didSelectRowAtIndexPath:
,这将允许您询问 tableview 哪个单元格被选中,然后采取适当的操作。一个例子是:
- (void)tableView:(UITableView *)tableView didSelectRowAtIndexPath:(NSIndexPath *)indexPath
{
UITableViewCell *cell = [tableView cellForRowAtIndexPath:indexPath];
if ([cell.textLabel.text isEqualToString:@"google"]) {
// open google
} else if ([cell.textLabel.text isEqualToString:@"yahoo"]) {
// open yahoo
}
}
编辑以回答评论中提出的问题
你没有说明这一点,但从阅读你的问题来看,我猜你正在使用单独的 nib 文件,并希望在用户选择其中一个静态单元格时在屏幕上推送另一个控制 Web View 的 View Controller 。执行此操作的步骤是:
- 创建新的 VC
- 为公共(public)属性提供您要加载的 URL
- 将 VC 推送到屏幕上
代码看起来像这样:
WebViewController webVC = [[WebViewController alloc] initWithNibName:@"your nib name" bundle:[NSBundle mainBundle]];
webVC.url = // some NSURL object, or maybe just a string that has the URL - that's up to you
[self.navigationController pushViewController:webVC animated:YES]
关于iphone - UITableViewController 中的静态单元格在同一 Nib 上打开不同的 URL,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/9715644/