简体   繁体   English

自定义UICollectionViewCell(collectionviewcell内的tableview)

[英]Custom UICollectionViewCell (tableview inside collectionviewcell)

I am new at this and I heard a lot the term “custom Collectionviewcell” when looking how to make a calendar in which squares have a list of events (like google calendar), because I read I could use a custom collectionviewcell to have a tableview inside each collectionviewcell. 我是新来的人,在寻找如何制作一个带有正方形事件列表的日历时(例如Google日历),我听到了很多“自定义Collectionviewcell”一词,因为我读到我可以使用一个自定义collectionviewcell进行表格浏览在每个collectionviewcell中。 The thing is that I looked for it and I still don't know what it is or how to implement it. 问题是我一直在寻找它,但我仍然不知道它是什么或如何实现它。 Anyone does? 有人吗 Thanks, Mateo. 谢谢,Mateo。

What you want looks like this. 您想要的看起来像这样。 But I would recommend looking at tutorials for UICollectionView and UITableView, and slowly working your way up to what you want to implement. 但是我建议您查看UICollectionView和UITableView的教程,并逐步按照自己的方式进行实现。 Hopefully you can grasp the concept of this. 希望您能理解这一概念。 But in theory this is how you implement a tableview inside a uicollectionviewCell. 但是从理论上讲,这就是在uicollectionviewCell中实现表格视图的方式。

First a class 第一堂课

    class CollectionViewController: UICollectionViewCOntroller, UICollectionViewDelegateFlowLayout {

        private let collectionViewCellIdentifier = "myCell"            

        override func viewDidLoad() {
             super.viewDidLoad()


        }

override func collectionView(_ collectionView: UICollectionView, numberOfItemsInSection section: Int) -> Int {
    return 1
}


override func collectionView(_ collectionView: UICollectionView, cellForItemAt indexPath: IndexPath) -> UICollectionViewCell {

    let cell = collectionView.dequeueReusableCell(withReuseIdentifier: collectionViewCellIdentifier, for: indexPath) as! YourCustomCell

    return cell
}

Then you want you make another Class 那你想再上一堂课

    class YourCustomCell: UICollectionViewCell {

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

    return tv
     }()

    }


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

  }

and then a Extension of YOurCustumCell: 然后是YOurCustumCell的扩展:

    extension ReceivedCell: UITableViewDataSource {

func tableView(_ tableView: UITableView, numberOfRowsInSection section: Int) -> Int {

    return 1 

    }

func tableView(_ tableView: UITableView, cellForRowAt indexPath: IndexPath) -> UITableViewCell {

    let cell = tableView.dequeueReusableCell(withIdentifier: cellIdentifier, for: indexPath) as! UITableViewCell // Or perhaps a custom tableview cell class swell


    return cell
}

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM