【问题标题】:How to embed some tableViews with different data in a collectionView?如何在collectionView中嵌入一些具有不同数据的tableView?
【发布时间】: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


    【解决方案1】:

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

    【讨论】:

    • 我遇到了两个错误:第一个在 viewController 中。我应该为 numberOfRowsInSection 返回什么?另一个也在 viewController 中。它说“'UITableViewCell'类型的值没有成员'dataSource'”。
    猜你喜欢
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 2015-10-13
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    相关资源
    最近更新 更多