我是 iOS 及其开发的新手。我正在开发 iPhone 应用程序。我有 UITableView
和 UIWebView
。当我按下 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/