iphone - UITableViewController 中的静态单元格在同一 Nib 上打开不同的 URL

标签 iphone ios xcode uitableview uiwebview

我对 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 。执行此操作的步骤是:

  1. 创建新的 VC
  2. 为公共(public)属性提供您要加载的 URL
  3. 将 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/

相关文章:

ios - CoreData 中的删除不会删除;重新启动后出现删除的对象

iphone - iOS 每天在特定时间触发本地通知

ios - 构建后 XCode iOS 颜色不匹配

iphone - 如何通过单元格的文本数据获取 NSIndexPath(对于表格单元格)?

iphone - 如何通过 objective-c 中的编码将pdf文件转换为图像

iphone - NSPredicate 通过关系而不是属性来过滤数据

iphone - 动态地将 UITableView 分成任意部分?

Swift 结构初始化,使另一个结构像 String

iphone - scheduleLocalNotification 的性能问题(大量本地通知)

iphone - ASIHTTPRequest UIWebView加载文档