2016-09-23 82 views
0

我正在使用集合视图,我有6个单元格在集体视图中,我想单元格单击时呈现不同的网址,我该怎么做?在一系列网址的帮助下?还是有另一种方法来做到这一点?web视图不加载url

FirstViewController.m

-(void)viewDidLoad{ 
    array=[NSArray arrayWithObjects:@"home.jpg",@"about-us.png",@"aarti.png",@"video.png",@"news.png",@"contact.png", nil]; 


    NSString *[email protected]"url1"; 
    NSString *[email protected]"url2"; 
    NSString *[email protected]"url3"; 
    NSString *[email protected]"url4"; 
    NSString *[email protected]"url5"; 
    NSString *[email protected]"url6"; 

    urls=[NSArray arrayWithObjects:url1,url2,url3,url4,url5,url6, nil]; 

} 
- (NSInteger)collectionView:(UICollectionView *)collectionView numberOfItemsInSection:(NSInteger)section { 
    return array.count; 
} 

- (UICollectionViewCell *)collectionView:(UICollectionView *)collectionView cellForItemAtIndexPath:(NSIndexPath *)indexPath{ 

    static NSString *identifier = @"collect"; 

    UICollectionViewCell *cell = [collectionView dequeueReusableCellWithReuseIdentifier:identifier forIndexPath:indexPath]; 

    UIImageView *recipeImageView = (UIImageView *)[cell viewWithTag:100]; 
    recipeImageView.image = [UIImage imageNamed:[array objectAtIndex:indexPath.row]]; 

    return cell; 
} 

-(void)collectionView:(UICollectionView *)collectionView didSelectItemAtIndexPath:(NSIndexPath *)indexPath { 

    NSString *urlAddress = [urls objectAtIndex:indexPath.row];//url at clicked index from array 

    NSURL *url = [NSURL URLWithString:urlAddress]; 

    WebViewController *webView=[[WebViewController alloc]init]; 
    NSURLRequest *requestObj = [NSURLRequest requestWithURL:url]; 
    [webView.webview loadRequest:requestObj]; 

} 
+0

读任何MVC结构或教程的CollectionView – Shubhank

+0

@Shubhank是的,我有形成具有6个细胞的集合视图,现在我想提出关于Web视图6个网址,我已经创建的Web视图并成功呈现和URL但我不知道如何呈现不同的网址,尊重单元格的索引。 –

+0

这里有一个代表'didSelectItemAtIndexPath'的集合方法,您可以在这里获取位置的url并在webview中显示它,或者通过segue传递任何您想要的内容。 – Shubhank

回答

0

使用的CollectionView委派方法didSelectItemAtIndexPath和根据indexPath重载UIWebView与地址在indexPath.row从URL的阵列。

一样,

-(void)collectionView:(UICollectionView *)collectionView didSelectItemAtIndexPath:(NSIndexPath *)indexPath { 

    NSString *urlAddress = [arrUrl objectAtIndex:indexPath.row]//url at clicked index from array 

    //Create a URL object. 
    NSURL *url = [NSURL URLWithString:urlAddress]; 

    //URL Request Object 
    NSURLRequest *requestObj = [NSURLRequest requestWithURL:url]; 

    //Load the request in the UIWebView. 
    [YourWebViewObject loadRequest:requestObj]; 

} 
+0

我如何创建URL数组? –

+0

将网址字符串存储到数组中,与您的collectionview单元格数相同的网址@ Shikha –

+0

好吧谢谢:)太多@ronak –