在多个collectionviewcells中添加tableview

时间:2019-03-28 23:54:49

标签: ios uitableview uicollectionview swift4

我的应用遇到了问题。我已经成功制作了一个具有5个单元的CollectionView,它可以水平滚动。在每个单元格中,我都希望有一个表格视图,它将显示从Arduino接收的单独数据。但是我根本无法显示表格视图。

全部该视图中的代码已通过编程方式制作。

特别是有一个线程给我希望自己找到答案。我在这里尝试了接受的答案:How do I show a UITableView within a UICollectionViewCell 但是,我仍然没有工作。

由于我一般都不具备快速的经验,更不用说在代码中进行设计了,所以我怀疑存在一些约束问题,因为我以前曾经经历过。

这是我的收藏夹查看代码:

class ViewController: UICollectionViewController, UICollectionViewDelegateFlowLayout {

let cellId = "cellId"

 // Returns how many items in one sections
override func collectionView(_ collectionView: UICollectionView, numberOfItemsInSection section: Int) -> Int {
    return 5
}
override func collectionView(_ collectionView: UICollectionView, cellForItemAt indexPath: IndexPath) -> UICollectionViewCell {
    let cell = collectionView.dequeueReusableCell(withReuseIdentifier: cellId, for: indexPath)
    // displays different colors in all the 5 sections
    let colors: [UIColor] = [.blue, .green, .gray, .red, .orange]
    cell.backgroundColor = colors[indexPath.item]

    return cell
}

//Determines the size for each cell
func collectionView(_ collectionView: UICollectionView, layout collectionViewLayout: UICollectionViewLayout, sizeForItemAt indexPath: IndexPath) -> CGSize {

    return CGSize(width: view.frame.width , height: view.frame.height - 50 )
}
//Minimize the line spacing between the sections ... return 0 (no spacing)
func  collectionView(_ collectionView: UICollectionView, layout collectionViewLayout: UICollectionViewLayout, minimumLineSpacingForSectionAt section: Int) -> CGFloat {
    return 0
    }

然后是表格视图代码:

class ReceivedCell: UICollectionViewCell {


lazy var tableView: UITableView = {
    let tv = UITableView()
    tv.delegate = self
    tv.dataSource = self
    tv.translatesAutoresizingMaskIntoConstraints = false
    return tv
}()


let tableViewCell = "tableViewCell"
var array = ["test", "test1", "test2"]

override init(frame: CGRect) {
    super.init(frame: frame)
    backgroundColor = UIColor.blue
    tableView.register(UITableViewCell.self, forCellReuseIdentifier: tableViewCell)
    setupTableView()
    //setupViews()

}

 required init?(coder aDecoder: NSCoder) {
    fatalError("Init(coder:) has not been implemented")
}
extension ReceivedCell: UITableViewDelegate, UITableViewDataSource {

func tableView(_ tableView: UITableView, numberOfRowsInSection section: Int) -> Int {
    return array.count
}

func tableView(_ tableView: UITableView, cellForRowAt indexPath: IndexPath) -> UITableViewCell {
    let cell = tableView.dequeueReusableCell(withIdentifier: tableViewCell, for: indexPath)

    cell.textLabel?.text = array[indexPath.item]


    return cell
}
func tableView(_ tableView: UITableView, heightForRowAt indexPath: IndexPath) -> CGFloat {
    return 50
}

}

如果不清楚,请随时提出问题!我已经很接近完成我的应用程序了,我已经准备好所有其他代码进入表格视图,我只能够显示它。非常感谢大家!

编辑

I want to have a tableview in every sections 1 - 5

1 个答案:

答案 0 :(得分:0)

我添加了一些代码,以便以下vcs可以根据需要工作。

class ViewController: UICollectionViewController, UICollectionViewDelegateFlowLayout {

let cellId = "cellId"

override func viewDidLoad() {
    super.viewDidLoad()
    collectionView.register(ReceivedCell.self, forCellWithReuseIdentifier: cellId)
    // Do any additional setup after loading the view.
}


// Returns how many items in one sections
override func collectionView(_ collectionView: UICollectionView, numberOfItemsInSection section: Int) -> Int {
    return 5
}
override func collectionView(_ collectionView: UICollectionView, cellForItemAt indexPath: IndexPath) -> UICollectionViewCell {
    let cell = collectionView.dequeueReusableCell(withReuseIdentifier: cellId, for: indexPath) as! ReceivedCell
    print(cell.contentView.frame)
    // displays different colors in all the 5 sections
    let colors: [UIColor] = [.blue, .green, .gray, .red, .orange]
    cell.backgroundColor = colors[indexPath.item]

    return cell
}

//Determines the size for each cell
func collectionView(_ collectionView: UICollectionView, layout collectionViewLayout: UICollectionViewLayout, sizeForItemAt indexPath: IndexPath) -> CGSize {

    return CGSize(width: view.frame.width , height: view.frame.height - 50 )
}
//Minimize the line spacing between the sections ... return 0 (no spacing)
func  collectionView(_ collectionView: UICollectionView, layout collectionViewLayout: UICollectionViewLayout, minimumLineSpacingForSectionAt section: Int) -> CGFloat {
    return 0
}


 }
  class ReceivedCell: UICollectionViewCell {


lazy var tableView: UITableView = {
    let tv = UITableView()
    tv.delegate = self
    tv.dataSource = self
    tv.translatesAutoresizingMaskIntoConstraints = false
    return tv
}()


let tableViewCell = "tableViewCell"
var array = ["test", "test1", "test2"]

override func didMoveToSuperview() {
    super.didMoveToSuperview()
    NSLayoutConstraint.activate([
        tableView.topAnchor.constraint(equalTo: topAnchor, constant: 20),
        tableView.bottomAnchor.constraint(equalTo: bottomAnchor,constant:  -20),
        tableView.leftAnchor.constraint(equalTo: leftAnchor,constant: 20),
        tableView.rightAnchor.constraint(equalTo: rightAnchor,constant: -20),
        ])
}

override init(frame: CGRect) {
    super.init(frame: frame)
    backgroundColor = UIColor.blue
    addSubview(tableView)
    tableView.register(UITableViewCell.self, forCellReuseIdentifier: tableViewCell)
    tableView.reloadData()

   // setupTableView()
    //setupViews()

}

required init?(coder aDecoder: NSCoder) {
    fatalError("Init(coder:) has not been implemented")
}

}

extension ReceivedCell: UITableViewDelegate, UITableViewDataSource {

    func tableView(_ tableView: UITableView, numberOfRowsInSection section: Int) -> Int {
        return array.count
    }

    func tableView(_ tableView: UITableView, cellForRowAt indexPath: IndexPath) -> UITableViewCell {
        let cell = tableView.dequeueReusableCell(withIdentifier: tableViewCell, for: indexPath)

        cell.textLabel?.text = array[indexPath.item]


        return cell
    }
    func tableView(_ tableView: UITableView, heightForRowAt indexPath: IndexPath) -> CGFloat {
        return 50
    }}