2017-01-03 52 views
0

我希望每個行都能看到inventory中每個對象的每個allItems。現在,在我寫入indexPathOfCollectionView的地方,有0表示它實際上正在工作,但我不知道應該寫哪個變量才能從inventory中看到每個allItemsUITableView dataSource來自UICollectionViewCell中的Class對象

var inventory = [Item]() 

class Item { 

    var name: String! 
    var allItems: [String]! 

    init(name: String, allItems: [String]) { 
     self.name = name 
     self.allItems = allItems 
    } 

} 

收集細胞:

class ItemCollectionViewCell: UICollectionViewCell, UITableViewDelegate, UITableViewDataSource { 

    var inventory = [Item]() 

    @IBOutlet weak var testTableView: UITableView! 
    @IBOutlet weak var nameLabel: UILabel! 

    func configureCell(_ inventory: Item) { 

     nameLabel.text = inventory[indexPath.row] 
     testTableView.dataSource = self 

    } 


    func tableView(_ tableView: UITableView, numberOfRowsInSection section: Int) -> Int { 
     return inventory[indexPathOfCollectionView*].allItems.count 
    } 

    func numberOfSections(in tableView: UITableView) -> Int { 
     return 1 
    } 

    func tableView(_ tableView: UITableView, cellForRowAt indexPath: IndexPath) -> UITableViewCell { 

     var cell: UITableViewCell? = tableView.dequeueReusableCell(withIdentifier: "ItemsCell") 

    if(cell == nil){ 
     cell = UITableViewCell(style: UITableViewCellStyle.default, reuseIdentifier: "ItemsCell") 
    } 

     cell.textLabel?.text = inventory[indexPathOfCollection*].allItems[indexPath.row] 

     return cell 
    } 
} 

這是viewController

class VC: UIViewController, UICollectionViewDelegate, UICollectionViewDataSource { 

     func collectionView(_ collectionView: UICollectionView, numberOfItemsInSection section: Int) -> Int { 

      return inventory.count 

     } 

     func numberOfSections(in collectionView: UICollectionView) -> Int { 

      return 1 

     } 

     func collectionView(_ collectionView: UICollectionView, cellForItemAt indexPath: IndexPath) -> UICollectionViewCell { 

      let cell = collectionView.dequeueReusableCell(withReuseIdentifier: "CollectionCellItem", for: indexPath) as! ItemCollectionViewCell 

      let it: Item! 
      it = inventory[indexPath.row] 

      cell.configureCell(it) 

      return cell 

     } 

    } 

這裏是什麼,我已經得到了

+0

那你到底要? – iYoung

+0

爲了能夠在'collectionViewCell'內的'tableView'中的Item中顯示每個獨立的數組。就像現在這樣,它只顯示第一個對象,正如我在庫存[0] .allItems [indexPath.row]中提到的那樣寫0。這是0的問題,我不能看到任何可能的解決方案 – Christian

+0

嘗試庫存[indexPath.row]'而不是'庫存[indexPathOfCollection *]' – iYoung

回答

1

從我可以告訴screenshot,從你的班級Item刪除Inventory陣列,因爲它不是nee代理裏面您的模型。它應該與UIViewController一起出現。進行以下更改應該使您的代碼按需要運行!

更改UIViewController TO-

class VC: UIViewController, UICollectionViewDelegate, UICollectionViewDataSource { 

    var inventory = [Item]() 

    func collectionView(_ collectionView: UICollectionView, numberOfItemsInSection section: Int) -> Int { 

     return inventory.count 
    } 

    func collectionView(_ collectionView: UICollectionView, cellForItemAt indexPath: IndexPath) -> UICollectionViewCell { 

     let cell = collectionView.dequeueReusableCell(withReuseIdentifier: "CollectionCellItem", for: indexPath) as! ItemCollectionViewCell 

     //Just pass the model inside of the array! 
     cell.itemsList = inventory[indexPath.item] 

     cell.configureCell() 

     return cell 
    } 
} 

而且裏面你UICollectionViewCell

class ItemCollectionViewCell: UICollectionViewCell, UITableViewDelegate, UITableViewDataSource { 

    var itemsList : Item! 

    @IBOutlet weak var testTableView: UITableView! 
    @IBOutlet weak var nameLabel: UILabel! 

    override func awakeFromNib() { 

     super.awakeFromNib() 

     testTableView.dataSource = self 
    } 

    func configureCell() { 

     testTableView.reloadData() 
    } 

    func tableView(_ tableView: UITableView, numberOfRowsInSection section: Int) -> Int { 
     return itemsList.allItems.count 
    } 

    func tableView(_ tableView: UITableView, cellForRowAt indexPath: IndexPath) -> UITableViewCell { 

     var cell: UITableViewCell? = tableView.dequeueReusableCell(withIdentifier: "ItemsCell") 

     if(cell == nil){ 

      cell = UITableViewCell(style: UITableViewCellStyle.default, reuseIdentifier: "ItemsCell") 
     } 

     //get array of strings from inside that particular model! 
     cell.textLabel?.text = itemsList.allItems[indexPath.row] 

     return cell 
    } 
} 
+0

當我處理您的答案時,我有點困惑,但最終我設法使它工作! numberOfRowsInSection使用itemsList.allItems.count,因爲itemsList是一個類,cellForRowAt是itemsList.allItems。謝謝你的啓發! – Christian

+0

啊,好的,趕上!我錯過了這一點很愚蠢! :) – Rikh

相關問題