自定义 UICollectionViewCell(collectionviewcell 内的 tableview)
Custom UICollectionViewCell (tableview inside collectionviewcell)
我是新手,在寻找如何制作一个方块中包含事件列表(如 google 日历)的日历时,我听到了很多术语“自定义 Collectionviewcell”,因为我读过我可以使用自定义 collectionviewcell 在每个 collectionviewcell 中有一个 tableview。
问题是我找过它,但我仍然不知道它是什么或如何实现它。有人吗?
谢谢,马特奥。
你想要的看起来像这样。但我建议您查看 UICollectionView 和 UITableView 的教程,然后慢慢地按照您想要实现的方式进行操作。希望你能掌握这个概念。但理论上这就是在 uicollectionviewCell 中实现 tableview 的方式。
先一个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
}
那你要再做一个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
}
我是新手,在寻找如何制作一个方块中包含事件列表(如 google 日历)的日历时,我听到了很多术语“自定义 Collectionviewcell”,因为我读过我可以使用自定义 collectionviewcell 在每个 collectionviewcell 中有一个 tableview。 问题是我找过它,但我仍然不知道它是什么或如何实现它。有人吗? 谢谢,马特奥。
你想要的看起来像这样。但我建议您查看 UICollectionView 和 UITableView 的教程,然后慢慢地按照您想要实现的方式进行操作。希望你能掌握这个概念。但理论上这就是在 uicollectionviewCell 中实现 tableview 的方式。
先一个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
}
那你要再做一个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
}