我对此很陌生,在寻找如何制作一个日历,其中方 block 有一个事件列表(如谷歌日历)时,我听到了很多“自定义 Collectionviewcell”这个术语,因为我读到我可以使用自定义 CollectionViewcell 来每个 Collection View 单元内都有一个表格 View 。 问题是我寻找了它,但我仍然不知道它是什么或如何实现它。有人这样做吗? 谢谢,马特奥。
最佳答案
你想要的看起来像这样。但我建议您查看 UICollectionView 和 UITableView 的教程,并慢慢地实现您想要实现的内容。希望您能掌握这个概念。但理论上,这就是在 uicollectionviewCell 中实现 tableview 的方式。
首先上课
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
}
然后你想再做一个类(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)
])
}
}
然后是 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
}
关于ios - 自定义 UICollectionViewCell(collectionviewcell 内的 tableview),我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/56998505/