1

Ich habe eine einfache UICollectionViewController, die X Menge an Zellen zurückgibt. Ich möchte es so haben, wenn eine Zelle ausgewählt wird, wird diese spezifische ausgewählte Zelle ihre Größe ändern und in der Höhe größer werden, da alle anderen Zellen gleich bleiben. Wie erreiche ich das? Hier ist mein Code:Ändern der Größe der ausgewählten UICollectionView-Zelle

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) 
    } 
} 

Antwort

8

Sie können überprüfen, ob die indexPath von sizeForItemAt ausgewählt ist. Wenn dies der Fall ist, geben Sie eine andere Höhe zurück, andernfalls geben Sie die Standardhöhe zurück.

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

Könnten Sie bitte ein Beispiel geben? Vielen Dank. –

+0

Sure arbeitet jetzt an einem – Callam

+0

Danke Mann. Schätze es wirklich. –

Verwandte Themen