UITableView与UIWebVIew不加载UIWebView。它需要IBAction吗?

时间:2013-08-02 15:43:07

标签: uitableview uiwebview

这是我到目前为止所获得的代码,基本上应该发生的是,一旦你在UITableView中做出选择,你应该得到一个过渡,将你带到UIWebView,虽然所有发生的事情都是你的选择它只是突出显示蓝色。

第一个视图控制器.h

#import <UIKit/UIKit.h>

@interface YoMaFifthViewController : UITableViewController

{

NSArray *data, *sites;
}


@end

第一个视图控制器.m

#import "YoMaFifthViewController.h"
#import "YoMaWebsiteViewController.h"

@interface YoMaFifthViewController ()

@end

@implementation YoMaFifthViewController



- (id)initWithStyle:(UITableViewStyle)style
{
self = [super initWithStyle:style];
if (self) {
    // Custom initialization
}
return self;

}

- (void)didReceiveMemoryWarning
{
[super didReceiveMemoryWarning];
// Dispose of any resources that can be recreated.

}

#pragma mark - view lifecycle

- (void)viewDidLoad
{
[super viewDidLoad];

data = [NSArray arrayWithObjects:@"Website", @"Developer", nil];
sites = [NSArray arrayWithObjects:
         @"https://www.google.co.uk/",
         @"https://www.google.co.uk/",
         nil];


// Uncomment the following line to preserve selection between presentations.
// self.clearsSelectionOnViewWillAppear = NO;

// Uncomment the following line to display an Edit button in the navigation bar for this view controller.
// self.navigationItem.rightBarButtonItem = self.editButtonItem;
}


#pragma mark - Table view data source

- (NSInteger)numberOfSectionsInTableView:(UITableView *)tableView
{

// Return the number of sections.
return 1;
}

- (NSInteger)tableView:(UITableView *)tableView numberOfRowsInSection:(NSInteger)section
{

// Return the number of rows in the section.
return [data count];
}

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

UITableViewCell *cell = [tableView dequeueReusableCellWithIdentifier:CellIdentifier];
if (cell == nil) {
    cell = [[UITableViewCell alloc] initWithStyle:UITableViewCellStyleSubtitle reuseIdentifier:CellIdentifier];
}

// Configure the cell...

cell.textLabel.text = [data objectAtIndex:indexPath.row];
cell.detailTextLabel.text = [sites objectAtIndex:indexPath.row];
cell.imageView.image = [UIImage imageNamed:[data objectAtIndex:indexPath.row]];
cell.accessoryType = UITableViewCellAccessoryDisclosureIndicator;
return cell;





}

/*
// Override to support conditional editing of the table view.
- (BOOL)tableView:(UITableView *)tableView canEditRowAtIndexPath:(NSIndexPath *)indexPath
{
// Return NO if you do not want the specified item to be editable.
return YES;
}
*/

/*
// Override to support editing the table view.
- (void)tableView:(UITableView *)tableView commitEditingStyle:      (UITableViewCellEditingStyle)editingStyle forRowAtIndexPath:(NSIndexPath *)indexPath
{
if (editingStyle == UITableViewCellEditingStyleDelete) {
    // Delete the row from the data source
    [tableView deleteRowsAtIndexPaths:@[indexPath] withRowAnimation:UITableViewRowAnimationFade];
}   
else if (editingStyle == UITableViewCellEditingStyleInsert) {
    // Create a new instance of the appropriate class, insert it into the array, and add a new row to the table view
}   
}
*/

/*
// Override to support rearranging the table view.
- (void)tableView:(UITableView *)tableView moveRowAtIndexPath:(NSIndexPath *)fromIndexPath toIndexPath:(NSIndexPath *)toIndexPath
{
}
*/

/*
// Override to support conditional rearranging of the table view.
- (BOOL)tableView:(UITableView *)tableView canMoveRowAtIndexPath:(NSIndexPath *)indexPath
{
// Return NO if you do not want the item to be re-orderable.
return YES;
}
*/

#pragma mark - Table view delegate

- (void)tableView:(UITableView *)tableView didSelectRowAtIndexPath:(NSIndexPath *)indexPath

{
YoMaWebsiteViewController *wvc = [[YoMaWebsiteViewController alloc] initWithNibName:@"YoMaWebsiteViewController" bundle:nil];
wvc.site = [sites objectAtIndex:indexPath.row];
[self.navigationController pushViewController:wvc animated:YES];

}




@end

网站视图控制器.h

#import <UIKit/UIKit.h>

@interface YoMaWebsiteViewController : UIViewController
{

IBOutlet UIWebView *webview;

}

@property (retain) NSString *site;


@end

网站视图控制器.m

#import "YoMaWebsiteViewController.h"

@interface YoMaWebsiteViewController ()

@end

@implementation YoMaWebsiteViewController
@synthesize site;

- (id)initWithNibName:(NSString *)nibNameOrNil bundle:(NSBundle *)nibBundleOrNil
{
self = [super initWithNibName:nibNameOrNil bundle:nibBundleOrNil];
if (self) {
    // Custom initialization
}
return self;
}

- (void)viewDidLoad
{
[super viewDidLoad];

NSURL *url = [NSURL URLWithString:self.site];
NSURLRequest *request = [NSURLRequest requestWithURL:url];
[webview loadRequest:request];

// Do any additional setup after loading the view from its nib.
}

- (void)didReceiveMemoryWarning
{
[super didReceiveMemoryWarning];
// Dispose of any resources that can be recreated.
}

@end

1 个答案:

答案 0 :(得分:0)

您需要IB动作或segue到网站视图控制器(对于静态单元格)。当谈到动态原型时,segues会起作用。你可以用一点代码来做segue。首先,从第一个视图控制器到网站视图控制器转到故事板和 Control -drag(使用底部的图标或使用文档大纲)。然后,在给segue一个标识符后,使用以下代码:

- (void)tableView:(UITableView *)tableView didSelectRowAtIndexPath:(NSIndexPath *)indexPath
{
    [self performSegueWithIdentifier:@"yourSegueIdentifier" sender:[self.tableView cellForRowAtIndexPath:indexPath]];
}

编辑2:如果您正在使用xib执行此操作(无控制拖动):

- (void)tableView:(UITableView *)tableView didSelectRowAtIndexPath:(NSIndexPath *)indexPath
{
    [self.navigationController pushViewController:viewControllerNameHere animated:YES sender:[self.tableView cellForRowAtIndexPath:indexPath]];
}