UICollectionView:从自定义单元格中更改 sizeForItemAtIndexPath [英] UICollectionView: change the sizeForItemAtIndexPath from within custom cell

查看:37
本文介绍了UICollectionView:从自定义单元格中更改 sizeForItemAtIndexPath的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

关于模型-视图-控制器 (MVC) 的一个非常基本的问题.

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

我有一个用自定义 UICollectionViewCells 填充的自定义 UICollectionView.集合视图在 UICollectionViewDelegateFlowLayout 方法中定义单元格的大小,collectionView: sizeForItemAtIndexPath.同时,通过自定义集合单元格视图控制器添加和删除子视图.

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?

推荐答案

您是否尝试过使用通知中心,

Have you tried using notification center,

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

在集合视图控制器的 viewDidLoad 中,

and in collection view controller's viewDidLoad,

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

然后实现选择器

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

有关NSNotificationCenter 的详细说明,请参阅此教程

For Detailed Explanation regarding NSNotificationCenter, See this Tutorial

希望这会有所帮助.谢谢

Hope this helps. Thanks

这篇关于UICollectionView:从自定义单元格中更改 sizeForItemAtIndexPath的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆