【问题标题】:UITableView dataSource from Class object within a UICollectionViewCellUICollectionViewCell 中 Class 对象的 UITableView 数据源
【发布时间】:2017-01-03 15:56:12
【问题描述】:

我希望看到inventory 中每个对象的每个allItems 都显示在每一行中。就像现在一样,在我写indexPathOfCollectionView 的地方,有一个 0 表示它实际上正在工作,但我不知道我应该写哪个变量才能看到来自inventory 的每个allItems

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

        }

    }

这是我所拥有的screenshot

【问题讨论】:

  • 你到底想要什么?
  • 能够在tableView 中的tableView 中显示Item 中的每个唯一数组。就像现在一样,它只显示第一个对象,正如我提到的在 inventory[0].allItems[indexPath.row] 中写入 0。 0 是问题,我看不到任何可能的解决方案
  • 尝试inventory[indexPath.row] 而不是inventory[indexPathOfCollection*]
  • 我得到相同的结果.. 它仍然显示第一个 inventory 中的对象。另外,如果我将indexPath.row 放入numberOfRowsInSection 会出现错误
  • 在你的课堂上Item 为什么要创建数组Inventory?在你的configureCell 中你没有通过模型?

标签: ios swift uitableview uicollectionview swift3


【解决方案1】:

据我所知,从您的班级 Item 中删除 Inventory 数组,因为它在您的模型内部中不需要。它应该与UIViewController 一起出现。进行以下更改应该可以使您的代码按要求运行!

将您的UIViewController 更改为-

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

【讨论】:

  • 我在处理你的答案时有点困惑,但最后我设法让它工作了! numberOfRowsInSection 与 itemsList.allItems.count 一起使用,因为 itemsList 是一个类,而 cellForRowAt 是 itemsList.allItems。多谢指教!
  • 啊,是的,不错!错过这个真是太愚蠢了! :)
猜你喜欢
  • 2015-11-05
  • 1970-01-01
  • 1970-01-01
  • 1970-01-01
  • 2012-06-25
  • 1970-01-01
  • 1970-01-01
  • 2016-08-08
相关资源
最近更新 更多