1

X 셀을 반환하는 간단한 UICollectionViewController가 있습니다. 셀을 선택하면 특정 셀이 크기가 변경되고 다른 모든 셀이 그대로 유지되므로 높이가 커집니다. 이것을 어떻게 성취합니까? 당신은 sizeForItemAt에서 indexPath이 선택되어 있는지 확인할 수 있습니다선택한 UICollectionView 셀의 크기 변경

class HomeController: UICollectionViewController, UICollectionViewDelegateFlowLayout { 

    let cellId = "cellId" 

    override func viewDidLoad() { 
     super.viewDidLoad() 
     collectionView?.backgroundColor = UIColor(white: 0.90, alpha: 1.0) 

     collectionView?.register(PostCell.self, forCellWithReuseIdentifier: cellId) 
     collectionView?.showsVerticalScrollIndicator = false 
     collectionView?.alwaysBounceVertical = true 
    } 

    override func collectionView(_ collectionView: UICollectionView, numberOfItemsInSection section: Int) -> Int { 
     return 4 
    } 

    override func collectionView(_ collectionView: UICollectionView, cellForItemAt indexPath: IndexPath) -> UICollectionViewCell { 
     let cell = collectionView.dequeueReusableCell(withReuseIdentifier: cellId, for: indexPath) as! PostCell 
     return cell 
    } 

    func collectionView(_ collectionView: UICollectionView, layout collectionViewLayout: UICollectionViewLayout, sizeForItemAt indexPath: IndexPath) -> CGSize { 
     let height = (view.frame.width) * 9/16 
     return CGSize(width: view.frame.width, height: height + 50 + 50) 
    } 
} 

답변

8

: 여기에 내 코드입니다. 이 경우 다른 높이를 반환하고 그렇지 않으면 표준 높이를 반환합니다.

override func collectionView(_ collectionView: UICollectionView, didSelectItemAt indexPath: IndexPath) { 

    collectionView.performBatchUpdates(nil, completion: nil) 
} 

func collectionView(_ collectionView: UICollectionView, layout collectionViewLayout: UICollectionViewLayout, sizeForItemAt indexPath: IndexPath) -> CGSize { 

    switch collectionView.indexPathsForSelectedItems?.first { 
    case .some(indexPath): 
     return CGSize() // your selected height 
    default: 
     let height = (view.frame.width) * 9/16 
     return CGSize(width: view.frame.width, height: height + 50 + 50) 
    } 
} 
+0

사례를 제공해 주시겠습니까? 고맙습니다. –

+0

지금 한 가지만 작업하십시오. – Callam

+0

감사합니다. 정말 감사합니다. –

관련 문제