本文介绍了UITableViewController中的静态单元格在同一笔尖上打开不同的URL的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我对UITableViewController很陌生,我想创建一个静态单元格UITableViewController,每个静态单元格都打开相同的nib文件,但UIWebView的URL将不同.例如第1行将打开google.com,而yahoo.com中将打开第2行.我可以知道该怎么做吗?

I am quite new to UITableViewController, I would like to make a static cells UITableViewController and each static cells open up the same nib file but the URL of the UIWebView will be different. e.g. row 1 will open google.com and row 2 in yahoo.com. May I know how can i do that?

谢谢

推荐答案

您将要实现tableview委托方法 tableView:didSelectRowAtIndexPath:,这将允许您询问tableview哪个单元格被选中,然后采取适当的措施.一个例子是:

You'll want to implement the tableview delegate method, tableView:didSelectRowAtIndexPath:, this will allow you to ask the tableview which cell was selected and then take the appropriate action. An example would be:

- (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视图的视图控制器.为此,请执行以下步骤:

You didn't state this, but from reading your question, I'm guessing you are using separate nib files and want to push another view controller on screen that controls a web view when the user selects one of the static cells. The steps to do this are to:

  1. 创建新的VC
  2. 将要加载的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]

这篇关于UITableViewController中的静态单元格在同一笔尖上打开不同的URL的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持!

10-30 07:33