SNos
SNos

Reputation: 3470

Collection view change cell size

I am trying to change the height of the cells in a UICollectionView when the cell is selected.

I managed to change the height however, when I select a cell the y position of the others does not change causing the cell selected to overlap the others.

How could tell the other cells to change y position the a cell height is modified?

func collectionView(collectionView: UICollectionView, didSelectItemAtIndexPath indexPath: NSIndexPath) {

        if let detailsCell = collectionView.cellForItemAtIndexPath(indexPath) as? UserDetailsCell {

            UIView.animateWithDuration(0.5, delay: 0, options: .TransitionCurlUp, animations: { 

                detailsCell.frame.size.height = 300

                }, completion: nil)

        }


        print(indexPath.row)
    }

Upvotes: 2

Views: 921

Answers (1)

Adriano Spadoni
Adriano Spadoni

Reputation: 4800

the issue here it that you are treating a single cell/item individualy, you should just use autolayout to do all the work.

Define the size of the cell/item on the delegate:

override func tableView(tableView: UITableView, heightForRowAtIndexPath indexPath: NSIndexPath) -> CGFloat {
    if let selectedRows = tableView.indexPathsForSelectedRows {
        if selectedRows.contains(indexPath) {
            return 300
        }
    }
    return 40
}

And in the selection, you just force an update on the layout.

override func tableView(tableView: UITableView, didSelectRowAtIndexPath indexPath: NSIndexPath) {
    tableView.beginUpdates()
    tableView.endUpdates()
}

Upvotes: 1

Related Questions