#ios #swift #uitableview #uicollectionview #tvos
#iOS #swift #uitableview #uicollectionview #tvos
Вопрос:
Я программно реализую CollectionView:
class collectionViews {
static func collectionViewOne() -> UICollectionView {
let flowLayout = CarouselFlowLayout()
let collectionViewOne = UICollectionView(frame: CGRect(x: 106, y: 313, width: 1708, height: 300), collectionViewLayout: flowLayout)
return collectionViewOne
}
}
Я отображаю его внутри tableViewCell из TableView, который также реализован программно:
class TableViewCell2: UITableViewCell {
var moviesItems: [movieItem] = []
let cellIdentifier = "movieCardCell"
let collectionViewOne = collectionViews.collectionViewOne()
private func setupCollectionView(){
collectionViewOne.delegate = self
collectionViewOne.dataSource = self
collectionViewOne.backgroundColor = UIColor (hex: "444A64")
collectionViewOne.register(UICollectionViewCell.self, forCellWithReuseIdentifier: "MyCell")
let nib = UINib(nibName: "movieCardCell", bundle: nil)
collectionViewOne.register(nib, forCellWithReuseIdentifier: cellIdentifier)
self.contentView.addSubview(collectionViewOne)
}
}
// These functions never get called
extension TableViewCell: UICollectionViewDelegate, UICollectionViewDataSource, UICollectionViewDelegateFlowLayout {
func collectionView(_ collectionView: UICollectionView, numberOfItemsInSection section: Int) -> Int {
print("INSIDE TableViewCell2.collectionView 1")
return 1
}
func collectionView(_ collectionView: UICollectionView, cellForItemAt indexPath: IndexPath) -> UICollectionViewCell {
let cell = collectionView.dequeueReusableCell(withReuseIdentifier: "collectionViewCell", for: indexPath)
print("INSIDE TableViewCell2.collectionView 2")
return cell
}
func collectionView(_ collectionView: UICollectionView, layout collectionViewLayout: UICollectionViewLayout, sizeForItemAt indexPath: IndexPath) -> CGSize {
// For some reason he chose the measures of collectionViewCell and substracted 2
print("INSIDE TableViewCell2.collectionView 3")
return CGSize(width: 139, height: 64)
}
}
Я хочу, чтобы размер фрейма CollectionView был равен размеру фрейма TableViewCell
:
Итак, есть идеи, что мне следует изменить здесь, если это возможно?
let collectionViewOne = UICollectionView(frame: CGRect(x: 106, y: 313, width: 1708, height: 300), collectionViewLayout: flowLayout)
Ответ №1:
Вы можете установить ограничения представления коллекции после добавления его в ячейку внутри вашего метода setupCollectionView ():-
//#MARK:- Table view cell
class YourTableViewCell: UITableViewCell {
var collectionView: UICollectionView!
override init(style: UITableViewCell.CellStyle, reuseIdentifier: String?) {
super.init(style: style, reuseIdentifier: reuseIdentifier)
self. setupCollectionView()
}
required init?(coder: NSCoder) {
super.init(coder: coder)
}
func setupCollectionView() {
// Set collection view
let layout = UICollectionViewFlowLayout()
collectionView = UICollectionView(frame: CGRect.zero, collectionViewLayout: layout)
collectionView.backgroundColor = .clear
collectionView.register(collectionCell.self, forCellWithReuseIdentifier: "collectionCell")
collectionView.showsVerticalScrollIndicator = false
collectionView.showsHorizontalScrollIndicator = false
self.contentView.addSubview(collectionView)
collectionView.delegate = self
collectionView.dataSource = self
//Here you can set constraint for collection view
collectionView.translatesAutoresizingMaskIntoConstraints = false
collectionView.leadingAnchor.constraint(equalTo: self.contentView.leadingAnchor, constant: 0).isActive = true
collectionView.trailingAnchor.constraint(equalTo: self.contentView.trailingAnchor, constant: 0).isActive = true
collectionView.topAnchor.constraint(equalTo: self.contentView.topAnchor, constant: 0).isActive = true
collectionView.bottomAnchor.constraint(equalTo: self.contentView.bottomAnchor, constant: 0).isActive = true
layout.scrollDirection = .horizontal
}
}
Ответ №2:
В CollectionViews.collectionViewOne() измените на:
static func collectionViewOne(frame: CGRect) -> UICollectionView {
let flowLayout = CarouselFlowLayout()
let collectionViewOne = UICollectionView(frame: frame, collectionViewLayout: flowLayout)
return collectionViewOne
}
и в TableViewCell2 вам придется изменить
let collectionViewOne = collectionViews.collectionViewOne()
Для
let collectionViewOne: UICollectionView!
и внутри setupCollectionViews()
collectionViewOne = collectionViews.collectionViewOne(frame: bounds)
Вы должны переместить его в эту функцию, потому что свойство экземпляра не может полагаться на self в его встроенном объявлении, т. Е. Вы не можете передавать границы в объявлении collectionViewOne .