Я следую https://www.freecodecamp.org/news/autolayout-programmatically-spotify-clone-in-swift/, чтобы создать uicollectionview, но другое - я использую UIView для реализации. Однако я не знаю, как щелкнуть каждую ячейку Sub-UICollectionCell и открыть новый UIController. Ниже мой код.
Class HomeView : UIView,UICollectionViewDataSource,UICollectionViewDelegate ,UICollectionViewDelegateFlowLayout{
lazy var collectionView : UICollectionView = {
let layout = UICollectionViewFlowLayout()
let cv = UICollectionView(frame: CGRect.zero, collectionViewLayout: layout)
cv.translatesAutoresizingMaskIntoConstraints = false
cv.delegate = self
cv.dataSource = self
cv.register(HomeCollectionViewCell.self, forCellWithReuseIdentifier: "cellId")
cv.backgroundColor = UIColor(hexString: "#F7F7F7")
return cv
}()
override init(frame: CGRect) {
super.init(frame: frame)
addSubview(collectionView)
}
func collectionView(_ collectionView: UICollectionView, numberOfItemsInSection section: Int) -> Int {
return sections.count
}
func collectionView(_ collectionView: UICollectionView, cellForItemAt indexPath: IndexPath) -> UICollectionViewCell {
let cell = collectionView.dequeueReusableCell(withReuseIdentifier: "cellId", for: indexPath) as! HomeCollectionViewCell
cell.section = sections[indexPath.item]
return cell
}
}
где у меня HomeCollectionCell как
class HomeCollectionViewCell : UICollectionViewCell , UICollectionViewDelegate, UICollectionViewDelegateFlowLayout,UICollectionViewDataSource{
override init(frame: CGRect) {
super.init(frame: frame)
addSubview(titleLabel)
collectionView.dataSource = self
collectionView.delegate = self
collectionView.register(SubHomeViewCell.self, forCellWithReuseIdentifier: cellId)
setupSubCells()
}
fileprivate func setupSubCells(){
// add collectionView to the view
addSubview(collectionView)
collectionView.dataSource = self
collectionView.delegate = self
}
func collectionView(_ collectionView: UICollectionView, numberOfItemsInSection section: Int) -> Int {
return self.subCategorys.count
}
func collectionView(_ collectionView: UICollectionView, cellForItemAt indexPath: IndexPath) -> UICollectionViewCell {
let cell = collectionView.dequeueReusableCell(withReuseIdentifier: cellId, for: indexPath) as! SubHomeViewCell
cell.subCategory = subCategorys[indexPath.item]
return cell
}
func collectionView(_ collectionView: UICollectionView, layout collectionViewLayout: UICollectionViewLayout, sizeForItemAt indexPath: IndexPath) -> CGSize {
let width = frame.height
let height = frame.height
return CGSize(width: width, height: height)
}
}
Для моего SubHomeView я просто добавляю addSubview изображения и заголовка Мне нужно щелкнуть каждую ячейку, чтобы открыть страницу контроллера, чтобы показать каждую деталь меню.