【发布时间】:2019-12-29 23:30:19
【问题描述】:
我正在为我在表格视图和集合视图之间的关联寻求您的帮助。
我有一个包含 6 个水平滚动单元格的 CollectionView。 在每个单元格中,我想放置一个表格视图。
对于每个 tableView,我需要知道将我想要的数据放在哪个单元格中。
实际上,我让我的代码可以在单元格内显示 tableView,但我无法找到我所在的集合视图单元格。
顺便说一句,我的 Meal_vc_cell 控制器实际上是空的,它只有一个简单的插座。没有别的了。
这是我的代码
class TrainFoodPlanViewController: BaseViewController, UICollectionViewDataSource, UICollectionViewDelegate, UICollectionViewDelegateFlowLayout, UITableViewDataSource, UITableViewDelegate {
let train_meals_list: [String] = ["breakfast", "snack_1", "pre_intra_post", "lunch", "snack_2", "diner"]
@IBOutlet weak var trainCollectionView: UICollectionView!
override func viewDidLoad() {
super.viewDidLoad()
self.title = "Food Plan"
}
func tableView(_ tableView: UITableView, numberOfRowsInSection section: Int) -> Int {
return 1
}
func tableView(_ tableView: UITableView, cellForRowAt indexPath: IndexPath) -> UITableViewCell {
return UITableViewCell()
}
func collectionView(_ collectionView: UICollectionView, numberOfItemsInSection section: Int) -> Int {
return train_meals_list.count
}
func collectionView(_ collectionView: UICollectionView, cellForItemAt indexPath: IndexPath) -> UICollectionViewCell {
let cell = collectionView.dequeueReusableCell(withReuseIdentifier: "collection_cell", for: indexPath) as! Meal_vc_cell
cell.meal_number.text = train_meals_list[indexPath.row]
return cell
}
}
【问题讨论】:
-
collection view cell应该是tableview数据源;您将适当的数组分配给集合视图的属性
-
您可以为单元格的 contentView 提供一个与其在 collectionView 中的位置相关联的标签(
cell.contentView.tag = indexPath.row),并将该标签提供给单元格内的 tableView(tableView.tag = cell.contentView.tag)。然后在您的 tableView 的委托中使用该标签来知道它与哪个 collectionViewCell 相关联(switch tableView.tag { ... }.
标签: ios swift uitableview uicollectionview tableview