Shikha Shikha - 1 year ago 81
Objective-C Question

web view not loading url

I am using Collection view, I have 6 cells in collective view and I want to present different url on cell click, how can i do it? with the help of array of urls? or is there another way to do it?


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

NSString *url1=@"url1";
NSString *url2=@"url2";
NSString *url3=@"url3";
NSString *url4=@"url4";
NSString *url5=@"url5";
NSString *url6=@"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];


Answer Source

Use CollectionView Delegate method didSelectItemAtIndexPath and according to indexPath reload UIWebView with Url at indexPath.row from array of Urls.


-(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];

Recommended from our users: Dynamic Network Monitoring from WhatsUp Gold from IPSwitch. Free Download