diff --git a/Hankkijogbo/Hankkijogbo/Present/HankkiDetail/View/Cell/HankkiMenuCollectionViewCell.swift b/Hankkijogbo/Hankkijogbo/Present/HankkiDetail/View/Cell/HankkiMenuCollectionViewCell.swift index fa53bf31..e2e0a0f5 100644 --- a/Hankkijogbo/Hankkijogbo/Present/HankkiDetail/View/Cell/HankkiMenuCollectionViewCell.swift +++ b/Hankkijogbo/Hankkijogbo/Present/HankkiDetail/View/Cell/HankkiMenuCollectionViewCell.swift @@ -9,7 +9,6 @@ import UIKit final class HankkiMenuCollectionViewCell: BaseCollectionViewCell { - // MARK: - Properties // MARK: - UI Components private var nameLabel: UILabel = UILabel() diff --git a/Hankkijogbo/Hankkijogbo/Present/HankkiDetail/View/HankkiDetailViewController.swift b/Hankkijogbo/Hankkijogbo/Present/HankkiDetail/View/HankkiDetailViewController.swift index 5d626eec..611a0c56 100644 --- a/Hankkijogbo/Hankkijogbo/Present/HankkiDetail/View/HankkiDetailViewController.swift +++ b/Hankkijogbo/Hankkijogbo/Present/HankkiDetail/View/HankkiDetailViewController.swift @@ -171,7 +171,6 @@ private extension HankkiDetailViewController { ) detailMapView.bindData(latitude: data.latitude, longitude: data.longitude) - menuCollectionView.updateLayout(menuSize: data.menus.count) menuCollectionView.collectionView.reloadData() } } @@ -370,10 +369,31 @@ extension HankkiDetailViewController: UICollectionViewDataSource, UICollectionVi func collectionView(_ collectionView: UICollectionView, cellForItemAt indexPath: IndexPath) -> UICollectionViewCell { guard let cell = collectionView.dequeueReusableCell(withReuseIdentifier: HankkiMenuCollectionViewCell.className, for: indexPath) as? HankkiMenuCollectionViewCell else { return UICollectionViewCell() } + if let data = viewModel.hankkiDetailData { cell.bindData(data.menus[indexPath.item]) + menuCollectionView.updateLayout() return cell } return UICollectionViewCell() } } + +extension HankkiDetailViewController: UICollectionViewDelegateFlowLayout { + + // 메뉴명 label 길이에 따라 다르게 셀 크기 지정 + func collectionView(_ collectionView: UICollectionView, layout collectionViewLayout: UICollectionViewLayout, sizeForItemAt indexPath: IndexPath) -> CGSize { + if let data = viewModel.hankkiDetailData { + let menuNameLabelSize = NSString(string: data.menus[indexPath.item].name) + .boundingRect( + with: CGSize(width: UIScreen.getDeviceWidth() - 44, height: CGFloat.greatestFiniteMagnitude), + options: .usesLineFragmentOrigin, + attributes: [NSAttributedString.Key.font: UIFont.setupPretendardStyle(of: .body8)], + context: nil + ) + return CGSize(width: UIScreen.getDeviceWidth(), height: menuNameLabelSize.height + 25) + } + + return .zero + } +} diff --git a/Hankkijogbo/Hankkijogbo/Present/HankkiDetail/View/HankkiMenuCollectionView.swift b/Hankkijogbo/Hankkijogbo/Present/HankkiDetail/View/HankkiMenuCollectionView.swift index e5cda280..25ecc0b0 100644 --- a/Hankkijogbo/Hankkijogbo/Present/HankkiDetail/View/HankkiMenuCollectionView.swift +++ b/Hankkijogbo/Hankkijogbo/Present/HankkiDetail/View/HankkiMenuCollectionView.swift @@ -12,7 +12,7 @@ final class HankkiMenuCollectionView: BaseView { // MARK: - UI Components private let flowLayout: UICollectionViewFlowLayout = UICollectionViewFlowLayout() - lazy var collectionView: UICollectionView = UICollectionView(frame: .init(x: 0, y: 0, width: UIScreen.getDeviceWidth(), height: 179), collectionViewLayout: flowLayout) + lazy var collectionView: UICollectionView = UICollectionView(frame: .zero, collectionViewLayout: flowLayout) // MARK: - Life Cycle @@ -20,9 +20,16 @@ final class HankkiMenuCollectionView: BaseView { addSubview(collectionView) } + override func setupLayout() { + collectionView.snp.makeConstraints { + $0.edges.equalToSuperview() + $0.width.equalTo(UIScreen.getDeviceWidth()) + $0.height.equalTo(179) + } + } + override func setupStyle() { flowLayout.do { - $0.itemSize = .init(width: UIScreen.getDeviceWidth(), height: 45) $0.scrollDirection = .vertical $0.minimumLineSpacing = 24 $0.headerReferenceSize = .init(width: UIScreen.getDeviceWidth(), height: 20 + 26) @@ -39,14 +46,11 @@ final class HankkiMenuCollectionView: BaseView { extension HankkiMenuCollectionView { - /// 메뉴 데이터 불러온 이후에 메뉴 개수에 따라 높이 동적으로 설정 - func updateLayout(menuSize: Int) { - collectionView.snp.removeConstraints() - collectionView.snp.makeConstraints { + func updateLayout() { + collectionView.snp.updateConstraints { $0.edges.equalToSuperview() $0.width.equalTo(UIScreen.getDeviceWidth()) - $0.height.equalTo(20 + 26 + 18 + (menuSize * (45 + 24)) + 48 + 18) + $0.height.equalTo(collectionView.contentSize.height) } - layoutIfNeeded() } }