我希望每個行都能看到inventory
中每個對象的每個allItems
。現在,在我寫入indexPathOfCollectionView
的地方,有0表示它實際上正在工作,但我不知道應該寫哪個變量才能從inventory
中看到每個allItems
。UITableView 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
}
}
這裏是什麼,我已經得到了
那你到底要? – iYoung
爲了能夠在'collectionViewCell'內的'tableView'中的Item中顯示每個獨立的數組。就像現在這樣,它只顯示第一個對象,正如我在庫存[0] .allItems [indexPath.row]中提到的那樣寫0。這是0的問題,我不能看到任何可能的解決方案 – Christian
嘗試庫存[indexPath.row]'而不是'庫存[indexPathOfCollection *]' – iYoung