Static cells in UITableViewController opening diff

2019-09-08 06:55发布

问题:

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]