Question

I have a CollectionView with UIImageView's in them and the images are defined in listImages array.

I am trying to get it so that when you click on a specified image, you will get redirected to a website with more information about that.

However, I don't know how to use the if clause to get the name of the image and by that give it a link.

- (void)viewDidLoad
{
    [super viewDidLoad];
    // Do any additional setup after loading the view.
    listImages = [NSArray arrayWithObjects:@"7513-kirjatkuivumassa.jpg", @"kuppi.jpg", @"kuva1.jpg", @"juna-042.jpg", @"rautio-valamonruusut-helleaamuna-maalaus.jpg", @"pysähtynyt1.jpg", @"Screen-Shot-2013-02-20-at-21.07.38.jpg", @"sateenkaari.jpg", @"Screen-Shot-2013-02-21-at-17.04.22.jpg", @"moninaiset-e1391026376696.jpg", @"Tomperi+Metsä20111.jpg", @"3-shinot.jpg", @"Ulpukat.jpg", @"janne-e1391025808211.jpg", @"martikainen-240x240.jpg", @"takala-240x240.jpg", @"paanukallokaarme1.jpg", @"käsityök-240x240.jpg", @"kuvis-004.jpg", @"Se-on-hieno-2012-tammi-105x28x223.jpg", nil];
}

- (void)collectionView:(UICollectionView *)collectionView didSelectItemAtIndexPath:(NSIndexPath *)indexPath
{
    UICollectionViewCell *cell = [collectionView cellForItemAtIndexPath:indexPath];
    if ([[cell.listImageView text] isEqualToString:@"7513-kirjatkuivumassa.jpg"]) {
        [[UIApplication sharedApplication] openURL:[NSURL URLWithString:@"http://website.com"]];
    }
}
Was it helpful?

Solution

please try this.

- (void)collectionView:(UICollectionView *)collectionView didSelectItemAtIndexPath:(NSIndexPath *)indexPath
{
    NSString *image_name= [listImages objectAtIndex:indexPath.row];
    if ([image_name isEqualToString:@"7513-kirjatkuivumassa.jpg"]) {
        [[UIApplication sharedApplication] openURL:[NSURL URLWithString:@"http://website.com"]];
    }
}

OTHER TIPS

I think your structure here is not very good. You should leverage the power of objective-C instead of trying to do things yourself. I would follow the following steps:

  1. Create an object with the following properties: (1) UIImage, (2) NSString with the text, (3) NSUrl with the URL.
  2. listImages should contain your objects
  3. Now it's very easy to do what you are trying to do

    - (void)collectionView:(UICollectionView *)collectionView didSelectItemAtIndexPath:(NSIndexPath *)indexPath
    {
             yourObject *yourObject = [listImages objectAtIndex:indexPath.row];
            [[UIApplication sharedApplication] openURL:yourObject.url]];
     }
    
Licensed under: CC-BY-SA with attribution
Not affiliated with StackOverflow
scroll top