2017-01-03 2 views
0

Ich möchte jedes allItems für jedes Objekt in inventory sehen, um in jeder Zeile angezeigt zu werden. Wie es jetzt ist, wo ich indexPathOfCollectionView geschrieben habe, gibt es eine 0, um zu sehen, dass es tatsächlich funktioniert, aber ich weiß nicht, welche Variable ich schreiben sollte, um jedes allItems aus dem inventory zu sehen.UITableView dataSource aus dem Klassenobjekt innerhalb einer UICollectionViewCell

var inventory = [Item]() 

class Item { 

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

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

} 

Sammlung Cell:

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 
    } 
} 

Dies ist in den 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 

     } 

    } 

ist hier ein screenshot von dem, was ich

+0

was wollen Sie genau? – iYoung

+0

Um jedes einzelne eindeutige Array innerhalb von 'Item' in der' tableView' innerhalb der 'collectionViewCell' anzeigen zu können. Wie es jetzt ist, zeigt es nur das erste Objekt an, wie ich erwähnte, 0 in Inventar [0] zu schreiben. AllItems [indexPath.row]. Das 0 ist das Problem, und ich kann keine mögliche Lösung sehen – Christian

+0

versuchen Sie 'inventory [indexPath.row] 'anstelle von' inventory [indexPathOfCollection *]' – iYoung

Antwort

1

Von habe, was ich sagen kann, von Ihre Klasse Item entfernen Sie die Inventory Array, wie es nicht notwendig ist ded innerhalb Ihr Modell. Es sollte mit der UIViewController vorhanden sein. Wenn Sie die folgenden Änderungen vornehmen, sollte Ihr Code wie erforderlich ausgeführt werden!

ändern 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 
    } 
} 

Und in Ihrem 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

Ich war ein wenig verwirrt, als ich mit Ihrer Antwort arbeitete , aber am Ende habe ich es geschafft zu arbeiten! numberOfRowsInSection hat mit itemsList.allItems.count gearbeitet, da es sich bei itemsList um eine Klasse und bei cellForRowAt um itemsList.allItems handelt. Danke, dass du den Weg aufgeklärt hast! – Christian

+0

Ahh ja gut fangen! Das war ziemlich dumm von mir, das zu verpassen! :) – Rikh

Verwandte Themen