【问题标题】:UITableView: click on cells and open the link in cells with UIWebViewUITableView:单击单元格并使用 UIWebView 打开单元格中的链接
【发布时间】:2016-03-14 18:48:27
【问题描述】:

我有一个带有书签按钮的UIWebView。书签保存在UITableView 中。单元格的名称是书签的链接。现在我想单击单元格,然后书签的 url 应该在我的 webview 中响亮。但它不起作用,当我点击单元格时。 这是我的代码:

- (UITableViewCell *)tableView:(UITableView *)tableView cellForRowAtIndexPath:(NSIndexPath *)indexPath {
    static NSString *cellIdentifier = @"Cell";

    UITableViewCell *cell = [tableView dequeueReusableCellWithIdentifier:cellIdentifier];
    if (cell == nil) {
        cell = [[UITableViewCell alloc] initWithFrame:CGRectZero reuseIdentifier:cellIdentifier];
    }
    cell.textLabel.text = [bookmarks objectAtIndex:indexPath.row];
    return cell;
}

-(void)tableView:(UITableView *)tableView didSelectRowAtIndexPath:(NSIndexPath *)indexPath {
    explorerView.urlField.text = [bookmarks objectAtIndex:indexPath.row];
    [explorerView textFieldShouldReturn:explorerView.urlField];
    [self.parentViewController dismissModalViewControllerAnimated:true];
}

你能给我一个代码示例如何做到这一点吗?这太棒了。

【问题讨论】:

    标签: ios objective-c uitableview uiwebview bookmarks


    【解决方案1】:

    您缺少实际代码,它将在UIWebView 中加载 URL。您应该在tableView:didSelectRowAtIndexPath:

    中添加类似以下代码的内容
    NSString *URLString = [bookmarks objectAtIndex:indexPath.row];
    NSURL *URL = [NSURL URLWithString: URLString];
    NSURLRequest *URLRequest = [NSURLRequest requestWithURL:URLRequest];
    [webView loadURLRequest:URLRequest];
    

    【讨论】:

    • 感谢您的快速回复,但我已经有此代码,但它不起作用
    • 或者它可能正在工作,但我看不到它,因为我的 uitableview 仍然存在。如何向用户显示带有 uiwebview 的视图控制器?
    • 嗯,我假设您的表视图控制器以UIWebView 显示在视图控制器上。在这种情况下,在[self.parentViewController dismissModalViewControllerAnimated:true] 调用之后,您应该会看到您的 Web 视图。顺便说一句,请使用YES(Objective-C)而不是true(C++)。
    • 不,事实并非如此。我有一个用于 webview 的 ViewController。然后我有一个滑出菜单,其中有一个名为“书签”的单元格。从这个单元格中,您可以到达带有所有书签的表格视图。从这里我想回到我的第一个带有 webview 的 viewcontroller,它显示了书签页面。对不起“真实”。我在目标 c 中非常新,我之前在 c++ 中编码
    • 我试过这个: UIStoryboard *storyboard = [UIStoryboard storyboardWithName:@"MainStoryboard" bundle:nil]; UIViewController *vc = [storyboard instantiateViewControllerWithIdentifier:@"Identifier"]; [self.navigationController pushViewController:vc 动画:YES];但现在我有一个线程 1:信号 SIGABRT 错误
    猜你喜欢
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 2012-05-06
    • 1970-01-01
    • 2014-11-20
    • 1970-01-01
    • 1970-01-01
    • 2013-06-11
    相关资源
    最近更新 更多