简体   繁体   中英

UIcollectionview Update cell height

Currently I am working on one dynamic layout of collection view.

On certain clicks inside collectionview cell (ex cell has UIbutton) the cell of that click button should increase height.

i have implemented the method of collectionviewflowlayoutdelegate SizeforItemAt:

When cell button click i am just reloading particular one indexpath self.collectionview.reloadItems([indexpath])

It will call SizeforItemAt: but it is calling for all cell again. So due to this my collectionview jerking all cells as it's sizes are updating.

So is there any way to update height for single cell only?

You can do like this

func collectionView(_ collectionView: UICollectionView, layout collectionViewLayout: UICollectionViewLayout, sizeForItemAt indexPath: IndexPath) -> CGSize{
    let width = collectionView.bounds.width
    let height = 0.0
    // for specific cell  height
    if indexPath.row == 1{
        height = 200.0 // chnage height for a specific cell
    }else{
        height = collectionView.bounds.height //actual height
    }
    return CGSize(width: width, height: width)
}

Update

I am not sure will it work or not when clicking on the button.

But you can change the height of a specific cell that is selected by implementing didSelectItemAt like the below.

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

    collectionView.performBatchUpdates(nil, completion: nil)

    // to reload specific cell use below line. 
    //collectionView.reloadItemsAtIndexPaths([indexPath])
}

You can Check if the idexPath in sizeForItemAt is selected or not. if it is selected then you can change the height otherwise return the original height. like the following.

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

    switch collectionView.indexPathsForSelectedItems?.first {
    case .some(indexPath):
        let height = 200.0 // here you can change the height for selected cell.
        return CGSize(width: view.frame.width,height: height) 
    default:
        let height = 200.0 // standard height
        return CGSize(width: view.frame.width, height: height)
    }
}

Check for collection view cell dynamic height.

And do not hard code height for cell, instead make the components inside the cell define the height of the cell. In this way when tap on button pragmatically increase/decrease the size of some component inside that cell and you would get the desired result.

So is there any way to update height for single cell only?

No. If sizeForItemAt: is implemented, it can and will be called for any cells the runtime wishes. It isn't up to you what cell this is. Your job is to return the correct value for whatever the cell in question is. If you don't want to change a cell's height, don't change it, but you can't stop the call from arriving.

However, I doubt that this has anything to do with your issue. It sounds like what you want is to update the height of this one cell is a smooth (animated) manner. That has nothing to do with what you asked.

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM