【问题标题】:Adding tableview inside multiple collectionviewcells在多个collectionviewcells中添加tableview
【发布时间】:2019-08-19 20:55:00
【问题描述】:

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

ALL此视图中的代码已以编程方式编写。

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

由于我一般对 swift 缺乏经验,在代码设计方面更是如此,我怀疑存在一些限制问题,因为我以前经历过。

这是我的收藏视图代码:

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
    }

然后是tableview代码:

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
}

}

如果有什么不清楚的地方欢迎提问!我非常接近完成我的应用程序,我已经准备好所有其余的代码进入 tableview,我只需要能够显示它。非常感谢各位!

编辑

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

【问题讨论】:

    标签: ios uitableview uicollectionview swift4


    【解决方案1】:

    我添加了一些代码,以便以下 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
        }}
    

    【讨论】:

    • 干杯,我会测试一下!
    • 我已经对此进行了测试,但我似乎仍然可以显示 tableview,请上传视图的图片
    • 我设法让它工作。似乎单元格的标识符不正确。改变它,它工作。简单的错误让我困惑了好几个小时。非常感谢!
    猜你喜欢
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 2021-12-04
    • 1970-01-01
    • 2016-09-07
    • 1970-01-01
    相关资源
    最近更新 更多