Reputation: 63
I have follow https://www.freecodecamp.org/news/autolayout-programmatically-spotify-clone-in-swift/ to create a uicollectionview but the different is I use UIView to implement. However I don't know how to click to each cell of the Sub-UICollectionCell and open a new UIController. Below is my code.
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
}
}
where I have HomeCollectionCell as
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)
}
}
For my SubHomeView I just add the addSubview of image and title I need to click each cell to open a controller page to show each menu detail.
Upvotes: 0
Views: 42
Reputation: 16341
Pass in a weak var
reference to your controller in HomeView
.Then, use didSelectItemAt
method of UICollectionView
inside your HomeView
, here is an example:
weak var homeViewController: HomeViewController?
override func collectionView(_ collectionView: UICollectionView, didSelectItemAt indexPath: IndexPath) {
let newViewController = UIViewController()
newViewController.view.backgroundColor = .purple
homeViewController?.navigationController?.pushViewController(newViewController, animated: true)
}
Upvotes: 1