繁体   English   中英

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

[英]Swift 2 passing data through segue from UIcollectionview (indexpath.row)

我试图通过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设置performeguewithidentifier

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

可以请人帮我吗?

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

我为您的问题尝试了解决方案,现在我明白了。请尝试以下解决方案。

在此之前,请转到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)
}

暂无
暂无

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM