首页 > 解决方案 > 如何在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
    }

}

标签: iosswiftxcodexcode11swift5

解决方案


像我所做的那样替换您的函数和变量,目前就像您对 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
    }

}

推荐阅读