ios - UITableView:单击单元格并使用 UIWebView 打开单元格中的链接

标签 ios objective-c uitableview uiwebview bookmarks

我有一个带有书签按钮的UIWebView。书签保存在 UITableView 中。单元格的名称是书签的链接。现在我想单击单元格,然后书签的网址应该在我的 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];
}

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

最佳答案

您缺少将在 UIWebView 中加载 URL 的实际代码。您应该将类​​似以下代码的内容添加到您的 tableView:didSelectRowAtIndexPath:

NSString *URLString = [bookmarks objectAtIndex:indexPath.row];
NSURL *URL = [NSURL URLWithString: URLString];
NSURLRequest *URLRequest = [NSURLRequest requestWithURL:URLRequest];
[webView loadURLRequest:URLRequest];

关于ios - UITableView:单击单元格并使用 UIWebView 打开单元格中的链接,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/35995637/

相关文章:

ios - iOS 中的内存管理,模拟器与 idevice 给出不同的结果

ios - 苹果 LLVM 6.0 错误 : clang failed with exit code -1

ios - 每次我的应用程序从后台返回时,我都需要向方法发送一条消息

带有 Objective C 示例的 iOS 模式,用于防止对方法的并发调用

ios - 如何修复表格 View 的搜索栏与状态栏重叠

arrays - iOS Swift - 如何获取数组的最后一项并将其附加到另一个数组以获取聊天消息

iphone - AVURLAsset 视频缓冲

ios - 以编程方式将 subview 添加到 UIView - ios

ios - 如何在 swift 中使用 .a 静态库?

ios - 如何在 UITableViews 中编辑多个部分的标题?