2017-10-16 75 views
0

我有像這樣一個結構..問題與添加到陣列而將數據傳遞到另一個視圖

struct Product { 
     let name : String 
     let id : String 

    } 
} 

我也有稱爲圖像的單獨的陣列arrayOfURLImages現在我具有collectionview並且每個collectionview的單元具有來自arrayOfURLImages的圖像以及顯示idrate的2個標籤。當我點擊collectionview的一個項目時,我加載了一個tableviewcell,其上有collectionview的圖像,還有idname

在點擊的CollectionView這是我如何傳遞imagename & id ...

func SellBtnTapped(_ sender: UIButton) { 

let indexPath = collectionView?.indexPath(for: ((sender.superview?.superview) as! RecipeCollectionViewCell)) 

self.photoThumbnail = self.arrayOfURLImages[(indexPath?.row)!] 

let myVC = storyboard?.instantiateViewController(withIdentifier: 「mydentifier") as! myViewController 
myVC.myImage = self.photoThumbnail 

let productObject = productData1[(indexPath?.row)!] 

myVC.prodName = productObject.name <— ‘name’ from struct 
myVC.prodId = productObject.id <— ‘id’ from struct 

navigationController?.pushViewController(myVC, animated: true) 
} 

現在我的問題是,我想補充這樣加載到一個數組的tableviewcells。此外,在從tableviewcell被加載在視圖 - 控制,這是怎麼了分配在cellForRowAt數據...

func tableView(_ tableView: UITableView, cellForRowAt indexPath: IndexPath) -> UITableViewCell { 
    let cell: sellTableViewCell = tableView.dequeueReusableCell(withIdentifier: "sellProductIdentifier") as! sellTableViewCell 

cell.IdLabel.text = prodId 
cell.nameLabel.text = prodName 

    return cell 
} 

目前,在numberOfRowsInSection,我只是給返回1,因此僅僅顯示一個細胞但我想我的手機添加到一個數組並獲得noOfItems爲陣的個性化......

希望有人能幫助... :)

編輯:

我真正想要實現的是,當我點擊一個集合視圖項時,會顯示一個tableviewcell,當我返回並單擊第二個集合視圖項時,應該顯示兩個tableview單元,每個tableview單元都具有來自第一個第二個collectionview單元格。

+0

不清楚你的問題。你想製作一組單元格並將其顯示正確? – Arrabidas92

+0

是的,@ Arrabidas92 ..但自從結構被使用我有點困惑... – bws

+0

你需要傳遞的不僅僅是一個結構到視圖控制器 –

回答

0

好像你想要的productData1傳遞到下一個的ViewController

簡單地使實例成員在myViewController

var arrProduct: [Product]? 

,並通過它,就像這樣:

myVC.arrProduct = productData1 

而且返回您的數組arrProduct.count的計數numberOfRowsInSection

,並在使用它的cellForRowAtIndexPath

let product = arrProduct[indexPath.row] 
cell.IdLabel.text = product.id 
cell.nameLabel.text = product.name 

編輯:

var selectedItems: [Product]? 
    func SellBtnTapped(_ sender: UIButton) { 

    let indexPath = collectionView?.indexPath(for: ((sender.superview?.superview) as! RecipeCollectionViewCell)) 

    self.photoThumbnail = self.arrayOfURLImages[(indexPath?.row)!] 

    let myVC = storyboard?.instantiateViewController(withIdentifier: 「mydentifier") as! myViewController 
    myVC.myImage = self.photoThumbnail 

    let productObject = productData1[(indexPath?.row)!] 
    if selectedItems == nil { 
     selectedItems = [Product(name:productObject.name, id:productObject.id)] 
    }else { 
     selectedItems.append(productObject) 
    } 
    myVC.arrProduct = selectedItems 

    navigationController?.pushViewController(myVC, animated: true) 
    } 

注:類名應以大寫字母和實例成員用小寫字母,而且名字應該開始是描述性的。

+0

但@Salman Ghumsani,我不能這樣做..var arrProduct :[產品]?因爲struct'Product'在我設置collectionview的viewcontroller中。它沒有在myViewController中定義。所以它會拋出未聲明類型的錯誤'產品' – bws

+0

不要擔心聲明它的文件級別。 –

+0

對不起,@薩爾曼Ghumsani ...也許我沒有得到你... :) – bws

相關問題