uicollectionviewflowlayoutautomaticsize uicollectionviewdelegateflowlayout systemlayoutsizefitting automatic ios uicollectionview resize uicollectionviewcell

ios - systemlayoutsizefitting - uicollectionviewdelegateflowlayout



Actualizar diseño de celda después de eliminar una celda en UICollectionView (1)

Creé un UICollectionView con cambio de tamaño dinámico de la altura de la celda. Estoy cambiando el tamaño de la celda de esta manera:

func collectionView(_ collectionView: UICollectionView, layout collectionViewLayout: UICollectionViewLayout, sizeForItemAt indexPath: IndexPath) -> CGSize { let approximatedwidthoftextview = view.frame.width - 70 let size = CGSize(width: approximatedwidthoftextview, height: 1000) let attributes = [NSAttributedStringKey.font: UIFont(name: "Avenir Next", size: 18)] let estimatedFrame = NSString(string: allnotes[indexPath.row].note).boundingRect(with: size, options: .usesLineFragmentOrigin, attributes: attributes, context: nil) return CGSize(width: view.frame.width, height: estimatedFrame.height + 38)

}

Cuando elimino una celda, el diseño de la celda restante se vuelve un poco extraño. El contenido se corta.

Curiosamente, navegar a un controlador de vista anterior y luego volver a este controlador corrige el diseño de la celda y las células vuelven a la normalidad.

¿Alguna idea de lo que está mal aquí?

import UIKit private let reuseIdentifier = "Cell" class AddedNotesCollectionViewController: UICollectionViewController, UICollectionViewDelegateFlowLayout { var allnotes = [notesarray]() override func viewDidAppear(_ animated: Bool) { print(allnotes.count) } override func viewDidLoad() { super.viewDidLoad() view.backgroundColor = UIColor(red:0.15, green:0.20, blue:0.22, alpha:1.0) // Uncomment the following line to preserve selection between presentations // self.clearsSelectionOnViewWillAppear = false // Register cell classes self.collectionView!.register(AddedNotesCollectionViewCell.self, forCellWithReuseIdentifier: reuseIdentifier) // Do any additional setup after loading the view. } override func didReceiveMemoryWarning() { super.didReceiveMemoryWarning() // Dispose of any resources that can be recreated. } override func collectionView(_ collectionView: UICollectionView, numberOfItemsInSection section: Int) -> Int { // #warning Incomplete implementation, return the number of items return allnotes.count } override func collectionView(_ collectionView: UICollectionView, didSelectItemAt indexPath: IndexPath) { var tag = indexPath.row print(tag) } override func collectionView(_ collectionView: UICollectionView, cellForItemAt indexPath: IndexPath) -> UICollectionViewCell { let cell = collectionView.dequeueReusableCell(withReuseIdentifier: reuseIdentifier, for: indexPath) as! AddedNotesCollectionViewCell // Configure the cell cell.deleteBtn.layer.setValue(indexPath.row, forKey: "index") cell.deleteBtn.addTarget(self, action: #selector(deleteUser(sender:)), for: UIControlEvents.touchUpInside) cell.backgroundColor = allnotes[indexPath.row].prioritycolor cell.textview.text = allnotes[indexPath.row].note // Remove the button from the first cell return cell } //deleting cell func @objc func deleteUser(sender:UIButton) { let i : Int = (sender.layer.value(forKey: "index")) as! Int allnotes.remove(at: i) addedNotesArrayGlobal.addednotesarrays.remove(at: i) collectionView?.reloadData() } override func viewDidLayoutSubviews() { super.viewDidLayoutSubviews() collectionView?.collectionViewLayout.invalidateLayout() } func collectionView(_ collectionView: UICollectionView, layout collectionViewLayout: UICollectionViewLayout, sizeForItemAt indexPath: IndexPath) -> CGSize { let approximatedwidthoftextview = view.frame.width - 70 let size = CGSize(width: approximatedwidthoftextview, height: 1000) let attributes = [NSAttributedStringKey.font: UIFont(name: "Avenir Next", size: 18)] let estimatedFrame = NSString(string: allnotes[indexPath.row].note).boundingRect(with: size, options: .usesLineFragmentOrigin, attributes: attributes, context: nil) return CGSize(width: view.frame.width, height: estimatedFrame.height + 32) }

}


Aunque el cambio de tamaño dinámico funciona bien en collectionView utilizando el algoritmo anterior, simplemente no está completo. Cambié a UITableView y usé diseños automáticos para hacer un cambio de tamaño dinámico. Es simple, fácil y funciona bastante bien.