2014-02-12 55 views
0

我有一個CollectionView和UIImageView在其中,圖像在listImages數組中定義。添加鏈接到基於圖像名稱的CollectionView單元格

我想知道它,所以當你點擊一個指定的圖像,你會被重定向到一個網站,更多的信息。

但是,我不知道如何使用if子句來獲取圖像的名稱,並通過它給它一個鏈接。

- (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"]]; 
    } 
} 
+0

你有數組中的網址嗎?如果是這樣,您只需獲取挖掘單元格的索引並查找數組中的URL,假定單元格按陣列中的對象順序顯示。 –

回答

0

請試試這個。

- (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"]]; 
    } 
} 
+0

謝謝。這按預期工作。 – ibab

0

我覺得你這裏的結構不是很好。你應該利用objective-C的力量,而不是試圖自己做事情。我將按照以下步驟操作:

  1. 創建一個具有以下屬性的對象:(1)UIImage,(2)帶有文本的NSString,(3)帶有URL的NSUrl。
  2. listImages應該包含你的對象
  3. 現在是很容易做你正在嘗試做

    - (void)collectionView:(UICollectionView *)collectionView didSelectItemAtIndexPath:(NSIndexPath *)indexPath 
    { 
         yourObject *yourObject = [listImages objectAtIndex:indexPath.row]; 
         [[UIApplication sharedApplication] openURL:yourObject.url]]; 
    } 
    
相關問題