ios - 根据不同的UITableViewCell点击加载不同的URL-iOS

标签 ios iphone objective-c uitableview uiwebview

我是 iOS 及其开发的新手。我正在开发 iPhone 应用程序。我有 UITableViewUIWebView。当我按下 UITableview 的单元格时,根据点击那里我想在我的 UIWebView 中加载各种网页。我该怎么做。请帮助我

举个例子

cell-one of tableview --> web page 1 in UIWebView
cell-two of tableview --> web page 2 in UIWebView

但在我的应用程序中,所有单元格都重定向到一个网页。请在下面找到我使用的代码。

tabelViewcontroller.m

![@interface Essentialinfocontroller ()
{

    NSMutableArray * titlearray;

    NSMutableArray * subtitilearray;

}
@end

@implementation Essentialinfocontroller

@synthesize info;



- (void)viewDidLoad
{
    \[super viewDidLoad\];
    self.info.delegate=self;
    self.info.dataSource=self;

    titlearray =\[\[NSMutableArray alloc\]initWithObjects:@"Custom",@"Department Of Immigration",@"Foriegn Currency Regulations",@"Sri Lankan Embassies" ,@"Sri Lankan Visa",@"When You Are There..", nil\];

}

-(NSInteger)numberOfSectionsInTableView:(UITableView *)tableView

{

    return 1;

}

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

    return \[titlearray count\];

}

-(UITableViewCell *)tableView:(UITableView *)tableView cellForRowAtIndexPath:(NSIndexPath *)indexPath
{

    static NSString * cellidentifier=@"cell";

    UITableViewCell * cell=

    \[info dequeueReusableCellWithIdentifier:cellidentifier forIndexPath:indexPath\];

    cell.textLabel.text =\[titlearray objectAtIndex:indexPath.row\];

    return cell;



}



-(void)prepareForSegue:(UIStoryboardSegue *)segue sender:(id)sender
{
    if(\[segue.identifier isEqualToString:@"custom"\])
    {


    }
}


@end][1]

detailviewController.m

- (void)viewDidLoad
{
    [super viewDidLoad];
    // Do any additional setup after loading the view.
    NSURL *url= [NSURL URLWithString:@"http://google.lk"];
    NSURLRequest * requestURL= [NSURLRequest requestWithURL:url];


    [webView  loadRequest:requestURL];


}

最佳答案

这里是你可以做的,首先在你的tableViewController.m中实现下面的方法

- (void)tableView:(UITableView *)tableView didSelectRowAtIndexPath:(NSIndexPath *)indexPath {
    [self performSegueWithIdentifier:[titlearray objectAtIndex:indexPath.row] sender:self];
}

然后是你的 prepareForSegue 方法

-(void)prepareForSegue:(UIStoryboardSegue *)segue sender:(id)sender
{
    DetailViewController *detailViewController = segue.destinationViewController;
    if([segue.identifier isEqualToString:@"custom"]) {
      detailViewController.urlString = @"Your Custom Url";
    } else if([segue.identifier isEqualToString:@"Department Of Immigration"]){
    detailViewController.urlString = @"Your Department Of Immigration Url";
    }
}

然后在 DetailViewController.h 中 添加以下属性

@property (nonatomic,strong) NSString *urlString;

在DetailViewController.m中

- (void)viewDidLoad
{
    [super viewDidLoad];
    // Do any additional setup after loading the view.
    NSURL *url= [NSURL URLWithString:self.urlString];
    NSURLRequest * requestURL= [NSURLRequest requestWithURL:url];
    [webView  loadRequest:requestURL];
}

关于ios - 根据不同的UITableViewCell点击加载不同的URL-iOS,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/24508216/

相关文章:

iphone - 如何实现单选按钮行为?

ios - XCode 仪器分配 : Look at Live Bytes or Overall Bytes?

iphone - 使用 XCode 4.2 (IOS-IPhone dev) 本地化字符串

iphone - 通过应用程序调用电话

ios - 如何确定方向?

iphone - NSClassFromString() 返回一个类,即使类不可用

ios - 容器中的导航栏

objective-c - 将 NSCoding 与 Realm 模型对象一起使用

ios - 需要访问包含CLLocation信息的NSArray对象

iphone - Sharekit - 发送 url 链接到 Facebook