【发布时间】:2013-01-12 02:42:57
【问题描述】:
是否有一种简单的方法可以仅在 UICollectionViewFlowLayout 中重新加载补充视图?我不想重新加载整个事情。有可能吗?
【问题讨论】:
标签: iphone ios objective-c ipad
是否有一种简单的方法可以仅在 UICollectionViewFlowLayout 中重新加载补充视图?我不想重新加载整个事情。有可能吗?
【问题讨论】:
标签: iphone ios objective-c ipad
查看文档后,我不确定是否要重新加载单个补充视图,但您可以使用 reloadSections: 更新 UICollectionView 中的 1 个或多个部分。 (documentation) 所以当节头需要更新时,可以调用:
目标-C:
[self.collectionView reloadSections:[NSIndexSet indexSetWithIndexesInRange:NSMakeRange(0, 3)]];
斯威夫特:
self.collectionView?.reloadSections(IndexSet(0 ..< 3))
碰巧调用:
- (UICollectionReusableView *)collectionView: (UICollectionView *)collectionView viewForSupplementaryElementOfKind:(NSString *)kind atIndexPath:(NSIndexPath *)indexPath {
//Place logic for handling update here...
}
【讨论】:
UICollectionViewFlowLayout 中是可选的。
如果您想从 Controller 更改补充视图的布局或大小,请使用[self.collectionView.collectionViewLayout invalidateLayout]。如果您只想刷新补充视图,请使用[supplementaryView setNeedsDisplay]。如果补充视图很复杂,请同时使用两者和/或将改变其尺寸。
【讨论】:
setNeedsDisplay() 有效,尤其是当您需要更改视图的样式时,例如颜色、文本、图像等。如果您的视图控制器使用UICollectionViewDataSource 协议,请使用viewForSupplementaryElementOfKind 方法将IndexPath 对象保存到视图控制器变量,然后使用yourCollectionView.supplementaryView() 获取它以刷新视图