【发布时间】: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