Anonymous
Вложенные представления коллекций с динамическим содержимым
Сообщение
Anonymous » 28 мар 2024, 14:53
Я использую два вложенных представления коллекций. Я добавил ChildCollectionView в ParentCollectionViewCell, и ChildCollectionView содержит 3 ячейки, но ParentCollectionViewCell не корректирует рамку ячейки в соответствии с содержимым.
Вот код,
ParentCollectionView
Код: Выделить всё
class ViewController: UIViewController {
var parentCollectionView: UICollectionView = {
let _collectionView = UICollectionView.init(frame: .zero, collectionViewLayout: UICollectionViewFlowLayout.init())
return _collectionView
}()
let id = "ParentCollectionViewCell"
override func viewDidLoad() {
super.viewDidLoad()
// Do any additional setup after loading the view.
view.addSubview(parentCollectionView)
parentCollectionView.translatesAutoresizingMaskIntoConstraints = false
NSLayoutConstraint.activate([
parentCollectionView.leadingAnchor.constraint(equalTo: view.leadingAnchor),
parentCollectionView.trailingAnchor.constraint(equalTo: view.trailingAnchor),
parentCollectionView.topAnchor.constraint(equalTo: view.topAnchor),
parentCollectionView.bottomAnchor.constraint(equalTo: view.bottomAnchor)
])
parentCollectionView.dataSource = self
parentCollectionView.register(UINib(nibName: id, bundle: nil), forCellWithReuseIdentifier: id)
if let flowLayout = parentCollectionView.collectionViewLayout as? UICollectionViewFlowLayout {
flowLayout.estimatedItemSize = UICollectionViewFlowLayout.automaticSize
}
}
}
extension ViewController: UICollectionViewDataSource {
func collectionView(_ collectionView: UICollectionView, numberOfItemsInSection section: Int) -> Int {
1
}
func collectionView(_ collectionView: UICollectionView, cellForItemAt indexPath: IndexPath) -> UICollectionViewCell {
let cell = collectionView.dequeueReusableCell(withReuseIdentifier: id, for: indexPath) as! ParentCollectionViewCell
cell.backgroundColor = .red
return cell
}
}
ParentCollectionViewCell
Код: Выделить всё
class ParentCollectionViewCell: UICollectionViewCell {
var childCollectionView: UICollectionView = {
let _collectionView = UICollectionView.init(frame: .zero, collectionViewLayout: UICollectionViewFlowLayout.init())
return _collectionView
}()
let reuseId = "ChildCollectionViewCell"
private let data = ["ChildCell1","ChildCell2","ChildCell3"]
override func awakeFromNib() {
super.awakeFromNib()
// Initialization code
setupViews()
}
func setupViews() {
contentView.addSubview(childCollectionView)
childCollectionView.translatesAutoresizingMaskIntoConstraints = false
NSLayoutConstraint.activate([
childCollectionView.leadingAnchor.constraint(equalTo: contentView.leadingAnchor),
childCollectionView.trailingAnchor.constraint(equalTo: contentView.trailingAnchor),
childCollectionView.topAnchor.constraint(equalTo: contentView.topAnchor),
childCollectionView.bottomAnchor.constraint(equalTo: contentView.bottomAnchor),
childCollectionView.widthAnchor.constraint(equalToConstant: 360)
])
childCollectionView.dataSource = self
childCollectionView.register(UINib(nibName: reuseId, bundle: nil), forCellWithReuseIdentifier: reuseId)
if let flowLayout = childCollectionView.collectionViewLayout as? UICollectionViewFlowLayout {
flowLayout.estimatedItemSize = UICollectionViewFlowLayout.automaticSize
}
}
}
extension ParentCollectionViewCell: UICollectionViewDataSource {
func collectionView(_ collectionView: UICollectionView, numberOfItemsInSection section: Int) -> Int {
data.count
}
func collectionView(_ collectionView: UICollectionView, cellForItemAt indexPath: IndexPath) -> UICollectionViewCell {
let cell = collectionView.dequeueReusableCell(withReuseIdentifier: reuseId, for: indexPath) as! ChildCollectionViewCell
cell.backgroundColor = .gray
cell.setupViews()
return cell
}
}
ChildCollectionViewCell
Код: Выделить всё
class ChildCollectionViewCell: UICollectionViewCell {
override func awakeFromNib() {
super.awakeFromNib()
// Initialization code
}
func setupViews() {
let label = UILabel()
label.text = "Child Collection"
label.numberOfLines = 0
label.font = label.font.withSize(50)
contentView.addSubview(label)
label.translatesAutoresizingMaskIntoConstraints = false
NSLayoutConstraint.activate([
label.leadingAnchor.constraint(equalTo: contentView.leadingAnchor),
label.trailingAnchor.constraint(equalTo: contentView.trailingAnchor),
label.topAnchor.constraint(equalTo: contentView.topAnchor),
label.bottomAnchor.constraint(equalTo: contentView.bottomAnchor)
])
}
}
Текущий результат
Текущий результат
Ожидаемый результат
Ожидаемый результат
Подробнее здесь:
https://stackoverflow.com/questions/736 ... ic-content
1711626823
Anonymous
Я использую два вложенных представления коллекций. Я добавил ChildCollectionView в ParentCollectionViewCell, и ChildCollectionView содержит 3 ячейки, но ParentCollectionViewCell не корректирует рамку ячейки в соответствии с содержимым. Вот код, [b]ParentCollectionView[/b] [code]class ViewController: UIViewController { var parentCollectionView: UICollectionView = { let _collectionView = UICollectionView.init(frame: .zero, collectionViewLayout: UICollectionViewFlowLayout.init()) return _collectionView }() let id = "ParentCollectionViewCell" override func viewDidLoad() { super.viewDidLoad() // Do any additional setup after loading the view. view.addSubview(parentCollectionView) parentCollectionView.translatesAutoresizingMaskIntoConstraints = false NSLayoutConstraint.activate([ parentCollectionView.leadingAnchor.constraint(equalTo: view.leadingAnchor), parentCollectionView.trailingAnchor.constraint(equalTo: view.trailingAnchor), parentCollectionView.topAnchor.constraint(equalTo: view.topAnchor), parentCollectionView.bottomAnchor.constraint(equalTo: view.bottomAnchor) ]) parentCollectionView.dataSource = self parentCollectionView.register(UINib(nibName: id, bundle: nil), forCellWithReuseIdentifier: id) if let flowLayout = parentCollectionView.collectionViewLayout as? UICollectionViewFlowLayout { flowLayout.estimatedItemSize = UICollectionViewFlowLayout.automaticSize } } } extension ViewController: UICollectionViewDataSource { func collectionView(_ collectionView: UICollectionView, numberOfItemsInSection section: Int) -> Int { 1 } func collectionView(_ collectionView: UICollectionView, cellForItemAt indexPath: IndexPath) -> UICollectionViewCell { let cell = collectionView.dequeueReusableCell(withReuseIdentifier: id, for: indexPath) as! ParentCollectionViewCell cell.backgroundColor = .red return cell } } [/code] [b]ParentCollectionViewCell[/b] [code]class ParentCollectionViewCell: UICollectionViewCell { var childCollectionView: UICollectionView = { let _collectionView = UICollectionView.init(frame: .zero, collectionViewLayout: UICollectionViewFlowLayout.init()) return _collectionView }() let reuseId = "ChildCollectionViewCell" private let data = ["ChildCell1","ChildCell2","ChildCell3"] override func awakeFromNib() { super.awakeFromNib() // Initialization code setupViews() } func setupViews() { contentView.addSubview(childCollectionView) childCollectionView.translatesAutoresizingMaskIntoConstraints = false NSLayoutConstraint.activate([ childCollectionView.leadingAnchor.constraint(equalTo: contentView.leadingAnchor), childCollectionView.trailingAnchor.constraint(equalTo: contentView.trailingAnchor), childCollectionView.topAnchor.constraint(equalTo: contentView.topAnchor), childCollectionView.bottomAnchor.constraint(equalTo: contentView.bottomAnchor), childCollectionView.widthAnchor.constraint(equalToConstant: 360) ]) childCollectionView.dataSource = self childCollectionView.register(UINib(nibName: reuseId, bundle: nil), forCellWithReuseIdentifier: reuseId) if let flowLayout = childCollectionView.collectionViewLayout as? UICollectionViewFlowLayout { flowLayout.estimatedItemSize = UICollectionViewFlowLayout.automaticSize } } } extension ParentCollectionViewCell: UICollectionViewDataSource { func collectionView(_ collectionView: UICollectionView, numberOfItemsInSection section: Int) -> Int { data.count } func collectionView(_ collectionView: UICollectionView, cellForItemAt indexPath: IndexPath) -> UICollectionViewCell { let cell = collectionView.dequeueReusableCell(withReuseIdentifier: reuseId, for: indexPath) as! ChildCollectionViewCell cell.backgroundColor = .gray cell.setupViews() return cell } } [/code] [b]ChildCollectionViewCell[/b] [code]class ChildCollectionViewCell: UICollectionViewCell { override func awakeFromNib() { super.awakeFromNib() // Initialization code } func setupViews() { let label = UILabel() label.text = "Child Collection" label.numberOfLines = 0 label.font = label.font.withSize(50) contentView.addSubview(label) label.translatesAutoresizingMaskIntoConstraints = false NSLayoutConstraint.activate([ label.leadingAnchor.constraint(equalTo: contentView.leadingAnchor), label.trailingAnchor.constraint(equalTo: contentView.trailingAnchor), label.topAnchor.constraint(equalTo: contentView.topAnchor), label.bottomAnchor.constraint(equalTo: contentView.bottomAnchor) ]) } } [/code] [b]Текущий результат[/b] Текущий результат [b]Ожидаемый результат[/b] Ожидаемый результат Подробнее здесь: [url]https://stackoverflow.com/questions/73623069/nested-collection-views-with-dynamic-content[/url]