UICollectionView:更改自定义单元格中的sizeForItemAtIndexPath
关于模型 – 视图 – 控制器(MVC)的一个非常基本的问题。
我有一个自定义UICollectionView填充自定义UICollectionViewCells
。 集合视图定义了UICollectionViewDelegateFlowLayout
方法中的单元格的大小,即collectionView: sizeForItemAtIndexPath
。 同时,通过自定义收集单元视图控制器添加和删除子视图。
我的问题:当从单元格的视图控制器添加或删除子视图,我怎么也告诉集合视图控制器更改高度?
我明白委托和数据源的概念,但认为单元格是它自己的委托,所以当添加子视图时,如何将消息(“更改高度…”)从单元传递到集合视图控制器。去掉?
你有没有尝试过使用通知中心,
[[NSNotificationCenter defaultCenter] postNotificationName:@"AddedSubViewToCell" object:self];
并在集合视图控制器的viewDidLoad中,
[[NSNotificationCenter defaultCenter] addObserver:self selector:@selector(addedSubViewToCell:) name:@"AddedSubViewToCell" object:nil];
然后执行select器
- (void)addedSubViewToCell:(NSNotification:)notificationObject { //change frame of collection view //don't forget to remove observer }
有关NSNotificationCenter
详细说明,请参阅本教程
希望这可以帮助。 谢谢