2016-09-23 2 views
0

Я использую Collection view, у меня есть 6 ячеек в коллективном представлении, и я хочу представить разные URL-адреса на клике, как я могу это сделать? с помощью массива URL-адресов? или есть другой способ сделать это?web view not loading 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 ячеек, и теперь я хочу представить 6 URLs на веб-просмотра, я создал веб-просмотра и успешно представляя и URL, но я не знаю, как представить разные URL-адреса, с учетом индекса ячейки. –

+0

есть метод делегата коллекции 'didSelectItemAtIndexPath' здесь вы можете получить URL-адрес позиции и показать ее в веб-просмотре или передать в любом случае, что хотите. – Shubhank

ответ

0

Применение CollectionView Делегат метод didSelectItemAtIndexPath и в соответствии с indexPath перезагрузкой UIWebView с Url в 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

хранить строки URL в массиве, URL-адреса с тем же подсчетом для вашего подсчета клеток коллекции @ Shikha –

+0

хорошо спасибо :) слишком много @ronak –