ios - 如何在collectionView中嵌入一些具有不同数据的tableView?
问题描述
现在,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
}
}
解决方案
像我所做的那样替换您的函数和变量,目前就像您对 data1、data2、data3 等所做的那样......您无法实现所需的功能。
let dataSource = [["A", "B", "C", "D"]
,["E", "F", "G", "H"],
["I", "J", "K", "L"],
["M", "N", "O", "P"]]
func collectionView(_ collectionView: UICollectionView, cellForItemAt indexPath: IndexPath) -> UICollectionViewCell {
let cell = collectionView.dequeueReusableCell(withReuseIdentifier: "mealCellID", for: indexPath) as! CollectionViewCell
cell.dataSource = dataSource[indexPath.item]
cell.tableView.delegate = self
cell.tableView.dataSource = self
return cell
}
func collectionView(_ collectionView: UICollectionView, numberOfItemsInSection section: Int) -> Int {
return dataSource.count
}
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 = cell?.dataSource[indexPath.row]
return cell!
}
import UIKit
class CollectionViewCell: UICollectionViewCell, UITableViewDelegate, UITableViewDataSource {
@IBOutlet weak var tableView: UITableView!
var dataSource = [String]()
func tableView(_ tableView: UITableView, numberOfRowsInSection section: Int) -> Int {
return dataSource.count
}
func tableView(_ tableView: UITableView, cellForRowAt indexPath: IndexPath) -> UITableViewCell {
let cell = tableView.dequeueReusableCell(withIdentifier: "dishCellID")!
return cell
}
}
推荐阅读
- google-sheets - 如何使用 IMPORTXML 公式引用引用中的单元格?
- entity-framework-core - 修复 EF Core 加载相关数据的预期行为是什么
- php - WooCommerce - 我的帐户页面将锚链接添加到注册部分
- python - 如何在 PySpark 中使用 UnaryTransformer?
- json - 访问 JSON 对象中的单个属性
- django - Django CMS - 无法重新排序子插件
- tensorflow - 创建空间变压器网络时无错误
- python - 在 Kaggle 上导入自己的 Python 模块
- linux - mkdir 函数的一半有效,但 cd 部分无效
- discord.py - 消息发送命令有问题