ios - UICollectionView:从自定义单元格中更改 sizeForItemAtIndexPath

标签 ios swift uicollectionview

关于模型- View - Controller (MVC) 的一个非常基本的问题。

我有一个自定义 UICollectionView,其中填充了自定义 UICollectionViewCells。 Collection View 在 UICollectionViewDelegateFlowLayout 方法 collectionView: sizeForItemAtIndexPath 中定义单元格的大小。同时,通过自定义集合单元格 View Controller 添加和删除 subview 。

我的问题:在单元格的 View Controller 中添加或删除 subview 时,如何同时告诉 Collection View Controller 更改高度?

我确实理解委托(delegate)和数据源的概念,但认为单元格是它自己的委托(delegate),那么当 subview 时,消息('将高度更改为...')如何从单元格传递到 Collection View Controller 添加/删除?

最佳答案

你试过通知中心吗,

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

在 Collection View Controller 的 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 的详细说明,请参阅 Tutorial

希望这对您有所帮助。 谢谢

关于ios - UICollectionView:从自定义单元格中更改 sizeForItemAtIndexPath,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/28109466/

相关文章:

swift - 如何在 macOS 中获取实时麦克风输入?

swift - 如何在 swift 中找到两个 unix 时间之间的分钟差异?

ios - UICollectionView 未注册单元格

objective-c - 如何在不调用 `layoutIfNeeded` 的情况下为约束设置动画?

iphone - 如何针对 NSConnection 消息确定 performSelectorOnMainThread 的优先级?

objective-c - iOS:替换数组数组中的对象

iOS viewDidLoad 未被触发

ios - 如何从 Swift 中的 .m3u8 格式视频 url 获取缩略图?

ios - 具有自定义单元格框架的 indexPathForItemAtPoint

ios - 如何在 Swift 中为 iOS 创建 SDK?