【问题标题】:Adding link to CollectionView cell based on image name根据图像名称添加指向 CollectionView 单元格的链接
【发布时间】:2014-02-12 11:57:06
【问题描述】:

我有一个包含 UIImageView 的 CollectionView,并且图像在 listImages 数组中定义。

我正在尝试获取它,以便当您单击指定图像时,您将被重定向到包含更多信息的网站。

但是,我不知道如何使用 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"]];
    }
}

【问题讨论】:

  • 你有那个数组中的 URL 吗?如果是这样,您将只获得点击单元格的索引并在数组中查找 URL,假设单元格按数组中对象的顺序显示。

标签: ios objective-c collectionview


【解决方案1】:

请试试这个。

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

【讨论】:

    【解决方案2】:

    我认为你这里的结构不是很好。您应该利用 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]];
       }
      

    【讨论】:

      猜你喜欢
      • 1970-01-01
      • 2021-09-28
      • 1970-01-01
      • 1970-01-01
      • 2015-01-17
      • 2014-10-18
      • 2016-06-21
      • 2020-07-22
      相关资源
      最近更新 更多