我希望看到 inventory
中每个对象的每个 allItems
都显示在每一行中。就像现在一样,在我编写 indexPathOfCollectionView
的地方,有一个 0 可以看出它确实在工作,但我不知道应该写哪个变量才能看到每个 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我所拥有的
最佳答案
据我所知,从您的类 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
}
}
关于ios - 来自 UICollectionViewCell 中的 Class 对象的 UITableView 数据源,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/41447382/