【发布时间】:2019-09-23 03:29:36
【问题描述】:
现在collectionView 中的所有四个tableView 都显示了data1 数组中的数据。如何在第一个 collectionViewCell 中显示 data1,在第二个 collectionViewCell 中显示 data2,依此类推?我是新手,所以请帮忙。
ViewController 中的当前代码:
import UIKit
class ViewController: UIViewController, UICollectionViewDelegate, UICollectionViewDataSource, UICollectionViewDelegateFlowLayout, UITableViewDelegate, UITableViewDataSource {
@IBOutlet weak var collectionView: UICollectionView!
let data1 = ["A", "B", "C", "D"]
let data2 = ["E", "F", "G", "H"]
let data3 = ["I", "J", "K", "L"]
let data4 = ["M", "N", "O", "P"]
override func viewDidLoad() {
super.viewDidLoad()
collectionView.delegate = self
collectionView.dataSource = self
}
func collectionView(_ collectionView: UICollectionView, numberOfItemsInSection section: Int) -> Int {
return 4
}
func collectionView(_ collectionView: UICollectionView, cellForItemAt indexPath: IndexPath) -> UICollectionViewCell {
let cell = collectionView.dequeueReusableCell(withReuseIdentifier: "mealCellID", for: indexPath) as! CollectionViewCell
cell.tableView.delegate = self
cell.tableView.dataSource = self
return cell
}
func collectionView(_ collectionView: UICollectionView,
layout collectionViewLayout: UICollectionViewLayout,
sizeForItemAt indexPath: IndexPath) -> CGSize {
return CGSize(width: collectionView.frame.width, height: collectionView.frame.height)
}
func tableView(_ tableView: UITableView, numberOfRowsInSection section: Int) -> Int {
return data1.count
}
func tableView(_ tableView: UITableView, cellForRowAt indexPath: IndexPath) -> UITableViewCell {
let cell = tableView.dequeueReusableCell(withIdentifier: "dishCellID")
cell?.textLabel?.text = data1[indexPath.row]
return cell!
}
}
还有 CollectionViewCell:
import UIKit
class CollectionViewCell: UICollectionViewCell, UITableViewDelegate, UITableViewDataSource {
@IBOutlet weak var tableView: UITableView!
func tableView(_ tableView: UITableView, numberOfRowsInSection section: Int) -> Int {
return section
}
func tableView(_ tableView: UITableView, cellForRowAt indexPath: IndexPath) -> UITableViewCell {
let cell = tableView.dequeueReusableCell(withIdentifier: "dishCellID")!
return cell
}
}
【问题讨论】:
标签: ios swift xcode xcode11 swift5