2016-01-25 52 views
0

我試圖通過segue傳遞數據(字典的值[「IDD」])。 (XCODE 7.2)Swift 2從UIcollectionview(indexpath.row)通過segue傳遞數據

這裏我的代碼:

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 
} 

但我不權瞭如何設置賽格瑞的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沒有問題,但是這是我的第一次收集查看

可以請別人幫我嗎?

親切的問候 非常感謝你 法比奧

回答

0

我想你的問題的解決,現在我得到it.Try解決方案如下。

在此之前,去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) 
} 
+0

嗨。謝謝你的答案,但可以使用相同的方法用於tableview? 這裏我的例子(它爲實現代碼如下) 覆蓋FUNC prepareForSegue(SEGUE:UIStoryboardSegue,發件人:AnyObject){ 如果(segue.identifier == 「homeDetailSegue」) { 讓datoDaPassare = segue.destinationViewController作爲! dettaglio 讓字典= arrRes [(self.tblJSONHOME.indexPathForSelectedRow?.row)!] datoDaPassare.setnomeOggetto(字典[ 「ID」]作爲!字符串) }} 謝謝 –