Swift 2通过UIcollectionview(indexpath.row)中的segue传递数据

时间:2016-01-25 17:05:37

标签: ios swift swift2 uistoryboardsegue

我试图通过segue传递数据(dict [" IDD"]的值)。 (XCODE 7.2)

这是我的代码:

var arrRes = [[String:AnyObject]]() 
var dict = [:] 

func collectionView(collectionView: UICollectionView, cellForItemAtIndexPath indexPath: NSIndexPath) -> UICollectionViewCell {

    let cell = collectionView.dequeueReusableCellWithReuseIdentifier("imageCell", forIndexPath: indexPath) as! collectionViewCell

    var dict = arrRes[indexPath.row]

    let newPrice = dict["price"]!.stringByReplacingOccurrencesOfString(".", withString: ",")

    cell.lblPrice.text = "€\(newPrice)"
    cell.lblTitle.text = dict["title"] as? String
    cell.lblBrand.text = dict["brand"] as? String
    cell.lblIDD.text = dict["IDD"] as? String

    cell.imageView!.image = UIImage(named: "loading.gif") //set placeholder image first.
    dispatch_async(dispatch_get_main_queue()) {
        cell.imageView!.downloadImageFrom(link: dict["image"] as! String, contentMode: UIViewContentMode.ScaleAspectFit) //set your image from link array.
    }

    return cell
}

但我不知道如何为segue设置performseguewithidentifier

override func prepareForSegue(segue: UIStoryboardSegue, sender: AnyObject?) {
    if(segue.identifier == "homeDetailSegue")
    {
        let cell = sender as! UICollectionViewCell
        let detailView = segue.destinationViewController as! dettaglio
        let dict = arrRes[(self.myCollectionView.indexPathForCell(cell))!]
        detailView.setnomeOggetto(arrRes["IDD"] as! String)
    }
}

我对tableviews没有任何问题,但这是我第一次使用collectionView

可以请别人帮帮我吗?

亲切的问候 非常感谢你 法比奥

1 个答案:

答案 0 :(得分:0)

我尝试了你的问题的解决方案,现在我得到了它。尝试下面的解决方案。

在此之前转到stroyborad,单击destinationViewController(此处为detailViewController)

Then click Identity Inspector of the Right Navigation bar.
Once you click the you can see the Identity under Custom Class
No click Identity then give 'detailsVC' or whatever you want in Storyboard ID. 

现在在CollectionViewDelegate方法

func collectionView(collectionView: UICollectionView, didSelectItemAtIndexPath indexPath: NSIndexPath) 
{
    // handle tap events
    print("You selected cell #\(indexPath.item)!")
    let detailVC = self.storyboard?.instantiateViewControllerWithIdentifier("detailVC") as! DetailViewController
    detailVC.passDataToLabel = String (indexPath.item )
    print(detailVC.passDataToLabel)
    self.navigationController?.pushViewController(detailVC, animated: true)
}