Created
September 21, 2022 13:41
-
-
Save likil33/457a288c925deb418e68e84fdda0e3b1 to your computer and use it in GitHub Desktop.
UICollectionviewSwift
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
| @IBOutlet weak var collectionviewList: UICollectionView! | |
| func baseSetup() { | |
| self.headerTitles.removeAll() | |
| collectionviewList.register(UINib(nibName: "TitleCollectionViewCell", bundle: nil), forSupplementaryViewOfKind: UICollectionView.elementKindSectionHeader, withReuseIdentifier: "TitleCollectionViewCell") | |
| collectionviewList.delegate = self | |
| collectionviewList.dataSource = self | |
| let layout = UICollectionViewFlowLayout() | |
| layout.sectionInset = UIEdgeInsets(top: 5, left: 0, bottom: 5, right: 15) | |
| layout.minimumInteritemSpacing = 0 | |
| layout.minimumLineSpacing = 0 | |
| self.collectionviewList?.collectionViewLayout = layout | |
| } | |
| func setupImageUploadCell() { | |
| self.baseSetup() | |
| page = "imageupload" | |
| collectionviewList.CVregister(cellType: ImageUploadCCell.self) | |
| collectionviewList.register(UINib(nibName: "ImgUploadFooterCCell", bundle: nil), forSupplementaryViewOfKind: UICollectionView.elementKindSectionFooter, withReuseIdentifier: "ImgUploadFooterCCell") | |
| collectionviewList.reloadData() | |
| } | |
| //MARK:- CollectionView delegateMenu | |
| extension RelationshipView: UICollectionViewDelegate, UICollectionViewDataSource { | |
| func numberOfSections(in collectionView: UICollectionView) -> Int { | |
| if headerHide { | |
| return 1 | |
| } | |
| else { | |
| return self.headerTitles.count | |
| } | |
| } | |
| func collectionView(_ collectionView: UICollectionView, numberOfItemsInSection section: Int) -> Int { | |
| if page == "imageupload" { | |
| if section == 0 {return 3} | |
| else if section == 1 {return 2} | |
| } | |
| return arrlist.count | |
| } | |
| func collectionView(_ collectionView: UICollectionView, cellForItemAt indexPath: IndexPath) -> UICollectionViewCell { | |
| if page == "imageupload" { | |
| let cell = collectionView.dequeueReusableCell(with: ImageUploadCCell.self, for: indexPath) | |
| cell.uploadImg.image = UIImage() | |
| if indexPath.section == 0 { | |
| if uploadImagesSection0.count > indexPath.row { | |
| cell.uploadImg.image = uploadImagesSection0[indexPath.row] | |
| }} | |
| else if indexPath.section == 1 { | |
| if uploadImagesSection1.count > indexPath.row { | |
| cell.uploadImg.image = uploadImagesSection1[indexPath.row] | |
| } | |
| } | |
| return cell | |
| } | |
| else { | |
| let cell = collectionView.dequeueReusableCell(with: SelectionListCCell.self, for: indexPath) | |
| let dddata = ListKeys.init(arrlist[indexPath.row]) | |
| cell.titleLbl.text = dddata.name | |
| if dddata.flag == true { | |
| cell.roundImage.image = UIImage(named: "roundSelect") | |
| } | |
| else { | |
| cell.roundImage.image = UIImage(named: "roundUnSelect") | |
| } | |
| return cell | |
| } | |
| } | |
| func collectionView(_ collectionView: UICollectionView, didSelectItemAt indexPath: IndexPath) { | |
| if page == "imageupload" { | |
| self.cameraAction?(indexPath.section, indexPath.row, true) | |
| } | |
| else { | |
| arrlist = backupArr | |
| arrlist[indexPath.row]["flag"] = true | |
| collectionviewList.reloadData() | |
| } | |
| } | |
| func collectionView(_ collectionView: UICollectionView, viewForSupplementaryElementOfKind kind: String, at indexPath: IndexPath) -> UICollectionReusableView { | |
| switch kind { | |
| case UICollectionView.elementKindSectionHeader: | |
| /* if page == "imageupload" { | |
| let headerView = collectionView.dequeueReusableSupplementaryView(ofKind: UICollectionView.elementKindSectionHeader, withReuseIdentifier: "TitleCollectionViewCell", for: indexPath) as! TitleCollectionViewCell | |
| if indexPath.section == 0 { | |
| headerView.titleLbl.text = userVM?.imageuploadTopImage | |
| headerView.titleLbl.textAlignment = .left | |
| titleHeight = headerView.titleLbl.frame.height | |
| } | |
| else { | |
| headerView.titleLbl.text = userVM?.CreateYourOwnMusical | |
| headerView.titleLbl.textAlignment = .left | |
| titleHeight = headerView.titleLbl.frame.height | |
| } | |
| headerView.titleLbl.sizeToFit() | |
| return headerView | |
| } | |
| else { | |
| let headerView = collectionView.dequeueReusableSupplementaryView(ofKind: UICollectionView.elementKindSectionHeader, withReuseIdentifier: "TitleCollectionViewCell", for: indexPath) as! TitleCollectionViewCell | |
| headerView.titleLbl.text = userVM?.relationshipTitle | |
| titleHeight = headerView.titleLbl.frame.height | |
| headerView.titleLbl.sizeToFit() | |
| return headerView | |
| } */ | |
| let headerView = collectionView.dequeueReusableSupplementaryView(ofKind: UICollectionView.elementKindSectionHeader, withReuseIdentifier: "TitleCollectionViewCell", for: indexPath) as! TitleCollectionViewCell | |
| headerView.titleLbl.text = "" | |
| if self.headerTitles.count > indexPath.section { | |
| headerView.titleLbl.text = self.headerTitles[indexPath.section] | |
| } | |
| headerView.titleLbl.textAlignment = .left | |
| titleHeight = headerView.titleLbl.frame.height | |
| headerView.titleLbl.sizeToFit() | |
| return headerView | |
| case UICollectionView.elementKindSectionFooter: | |
| if page == "imageupload" { | |
| let userFooter = collectionView.dequeueReusableSupplementaryView(ofKind: kind, withReuseIdentifier: "ImgUploadFooterCCell", for: indexPath) as! ImgUploadFooterCCell | |
| return userFooter | |
| } | |
| else { | |
| let cell = collectionView.dequeueReusableSupplementaryView(ofKind: UICollectionView.elementKindSectionHeader, withReuseIdentifier: "TitleCollectionViewCell", for: indexPath) as! TitleCollectionViewCell | |
| cell.titleLbl.text = "" | |
| return cell | |
| } | |
| default: | |
| return UICollectionViewCell() | |
| } | |
| } | |
| } | |
| extension RelationshipView: UICollectionViewDelegateFlowLayout { | |
| func collectionView(_ collectionView: UICollectionView, layout collectionViewLayout: UICollectionViewLayout, sizeForItemAt indexPath: IndexPath) -> CGSize { | |
| if page == "imageupload" { | |
| let numberOfItemsPerRow:CGFloat = 3 | |
| //let spacingBetweenCells:CGFloat = 0 | |
| //let totalSpacing = (2 * (spacingBetweenCells)) + ((numberOfItemsPerRow) * spacingBetweenCells) //Amount of total spacing in a row | |
| if let collection = self.collectionviewList{ | |
| let width = (collection.bounds.width - 15)/numberOfItemsPerRow | |
| return CGSize(width: width , height: width) | |
| } | |
| } | |
| else { | |
| if let collection = self.collectionviewList{ | |
| let width = collection.bounds.width | |
| return CGSize(width: width, height: 48) | |
| } | |
| } | |
| return CGSize(width: 0, height: 0) | |
| } | |
| func collectionView(_ collectionView: UICollectionView, layout collectionViewLayout: UICollectionViewLayout, referenceSizeForHeaderInSection section: Int) -> CGSize { | |
| let widthV = collectionView.frame.size.width | |
| let font = UIFont.systemFont(ofSize: 18) | |
| if headerHide { | |
| return CGSize(width: 0, height: 0) | |
| } | |
| if self.headerTitles.count > section { | |
| return CGSize(width:widthV, height:self.headerTitles[section].heightWithConstrainedWidth(width: widthV - 40, font:font)) | |
| } | |
| /* if page == "imageupload" { | |
| if section == 0 { | |
| return CGSize(width:widthV, height:userVM?.imageuploadTopImage.heightWithConstrainedWidth(width: widthV - 40, font:font) ?? 40) | |
| } | |
| else { | |
| return CGSize(width:widthV, height:userVM?.CreateYourOwnMusical.heightWithConstrainedWidth(width: widthV - 40, font:font) ?? 40) | |
| } | |
| } | |
| else { | |
| return CGSize(width:widthV, height:userVM?.relationshipTitle.heightWithConstrainedWidth(width: widthV - 40, font:font) ?? 40) | |
| } */ | |
| return CGSize(width: 0, height: 0) | |
| } | |
| func collectionView(_ collectionView: UICollectionView, layout collectionViewLayout: UICollectionViewLayout, referenceSizeForFooterInSection section: Int) -> CGSize | |
| { | |
| if page == "imageupload" && section == 1 { | |
| let collection = self.collectionviewList | |
| return CGSize(width: collection?.bounds.width ?? 0, height: 100) | |
| } | |
| return CGSize(width: 0, height: 0) | |
| } | |
| } | |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment