UICollectionView: change the sizeForItemAtIndexPat

2019-09-04 05:39发布

问题:

A very basic question about model-view-controller (MVC).

I have a custom UICollectionView filled with custom UICollectionViewCells. The collection view defines the size of the cells within the UICollectionViewDelegateFlowLayout method, collectionView: sizeForItemAtIndexPath. Meanwhile, subviews are added and removed via the custom collection cell view controller.

My question: when adding or removing subviews from the cell's view controller, how do I also tell the collection view controller to change the height?

I do understand delegate and data source concepts, but figure that the cell is it's own delegate, so how does the message ('change the height to...') get passed from the cell to the collection view controller when subviews are added/removed?

回答1:

Have you tried using notification center,

[[NSNotificationCenter defaultCenter] postNotificationName:@"AddedSubViewToCell" object:self];

and in collection view controller's viewDidLoad,

[[NSNotificationCenter defaultCenter] addObserver:self selector:@selector(addedSubViewToCell:) name:@"AddedSubViewToCell" object:nil];

and then implement the selector

- (void)addedSubViewToCell:(NSNotification:)notificationObject
{
     //change frame of collection view
     //don't forget to remove observer
}

For Detailed Explanation regarding NSNotificationCenter, See this Tutorial

Hope this helps. Thanks