In my project (UIKit, programmatic UI) I have a UITableView with sections. The cells use a custom class. On load all cells just show 3 lines of info (2 labels). On tap, all contents will be displayed. Therefor I've setup my custom cell class to have two containers, one for the 3 line preview and one for the full contents. These containers are added/removed from the cell's content view when needed when the user taps the cell by calling a method (toggleFullView) on the custom cell class. This method is called from the view controller in didSelectRowAt:
Basically it works, but there are some issues:
Would be great to at least no longer have multiple random cells expand on tap
Code Block func tableView(_ tableView: UITableView, didSelectRowAt indexPath: IndexPath) { let annotation = annotationsController.getAnnotationFor(indexPath) //Expandable cell guard let cell = tableView.cellForRow(at: indexPath) as? AnnotationCell else { return } cell.toggleFullView() tableView.reloadRows(at: [indexPath], with: .none) // tableView.reloadData() }
Basically it works, but there are some issues:
I have to double tap the cell for it to expand and again to make it collapse again. The first tap will perform the row animation of tableView.reloadRows(at: [indexPath], with: .none) and the second tap will perform the expanding. If I substitute reloadRows with tableView.reloadData() the expanding and collapsing will happen after a single tap! But that is disabling any animations obviously, it just snaps into place. How Do I get it to work with one tap?
When the cell expands, some other random cells are also expanded. I guess this has something to do with reusable cells, but I have not been able to remedy this.
I want to be the expanded cell to collapse once I tap another cell to expand, how do I perceive that?
Code Block [AnnotationCell full cell class](https://developer.apple.com/forums/content/attachment/caacbef0-2e20-4528-b86a-859d1ceb460f){: .log-attachment} import UIKit class AnnotationCell: UITableViewCell, SelfConfiguringAnnotationCell { //MARK: - Actions ///Expand and collapse the cell func toggleFullView() { showFullDetails.toggle() if showFullDetails { //show the full version if contentView.subviews.contains(previewDetailsView) { previewDetailsView.removeFromSuperview() } if !contentView.subviews.contains(fullDetailsView) { contentView.addSubview(fullDetailsView) } } else { //show the preview version if contentView.subviews.contains(fullDetailsView) { fullDetailsView.removeFromSuperview() } if !contentView.subviews.contains(previewDetailsView) { contentView.addSubview(previewDetailsView) } } UIView.animate(withDuration: 1.2) { self.layoutIfNeeded() } }
Would be great to at least no longer have multiple random cells expand on tap
Assuming your constraints / resizing mask can give UIKit enough info to correctly resize the cell, calling beginUpdates / endUpdates on the table view should cause it to resize any visible cells with animation. You shouldn’t actually need that animation block you’re currently using, I think.