ios - 如何在collectionView中嵌入一些具有不同数据的tableView?

标签 ios swift xcode xcode11 swift5

现在,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, 布局collectionViewLayout:UICollectionViewLayout, sizeForItemAt indexPath: IndexPath) -> CGSize { 返回CGSize(宽度:collectionView.frame.width,高度: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
    }

}

关于ios - 如何在collectionView中嵌入一些具有不同数据的tableView?,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/58055302/

相关文章:

swift - 如何检查 Any 值是否确认通用协议(protocol),例如整数类型

xcode - 安装字体不起作用

ios - 使用 Xcode 11.2 在 iOS 模拟器中打开 .app 时出错

ios - xib 文件没有 SafeArea 选项

ios - 应用程序标识符不匹配 - 应用程序 ID XCode

ios - 使用特定时区将 NSDate 转换为 Unix 时间戳

javascript - 在 native 文本输入中检测粘贴事件

ios - 我的 NSUserDefaults 代码不起作用

ios - Swift2 - 如何确定抛出错误的类型?

ios - 无法使用 UIbutton 呈现第二个 UIviewcontroller