Static cells in UITableViewController opening different URL on the same nib

浪子不回头ぞ 提交于 2019-12-12 04:55:38

问题


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?

Thanks


回答1:


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
    }
}

EDIT TO ANSWER THE QUESTION ASKED IN COMMENTS

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. Create the new VC
  2. Give the public property the URL you want to load
  3. Push the VC on screen

in code that will look something like:

 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]


来源:https://stackoverflow.com/questions/9715644/static-cells-in-uitableviewcontroller-opening-different-url-on-the-same-nib

易学教程内所有资源均来自网络或用户发布的内容,如有违反法律规定的内容欢迎反馈
该文章没有解决你所遇到的问题?点击提问,说说你的问题,让更多的人一起探讨吧!