我发现了一个关于如何制作粘性标题的博客,它的效果非常好.唯一的问题是我不认为它考虑了sectionInserts.
这是它的目的:

我有我的插页:
collectionViewFlowLayout.sectionInset = UIEdgeInsetsMake(16, 16, 16, 16);
Run Code Online (Sandbox Code Playgroud)
使用粘性标题,它向下移动16个像素:

我尝试使用原始代码进行调试,我认为问题出在最后一部分:
layoutAttributes.frame = (CGRect){
.origin = CGPointMake(origin.x, origin.y),
.size = layoutAttributes.frame.size
Run Code Online (Sandbox Code Playgroud)
如果我将其更改为origin.y - 16,则标题将从正确的位置开始,但是当向上推时,头部的16个像素会离开屏幕:

我不知道如何让它考虑到sectionInsects.有人可以帮忙吗?
以下是博客中的完整代码:
- (NSArray *) layoutAttributesForElementsInRect:(CGRect)rect {
NSMutableArray *answer = [[super layoutAttributesForElementsInRect:rect] mutableCopy];
UICollectionView * const cv = self.collectionView;
CGPoint const contentOffset = cv.contentOffset;
NSMutableIndexSet *missingSections = [NSMutableIndexSet indexSet];
for (UICollectionViewLayoutAttributes *layoutAttributes in answer) {
if (layoutAttributes.representedElementCategory == UICollectionElementCategoryCell) {
[missingSections addIndex:layoutAttributes.indexPath.section];
}
}
for (UICollectionViewLayoutAttributes *layoutAttributes in answer) {
if ([layoutAttributes.representedElementKind isEqualToString:UICollectionElementKindSectionHeader]) …Run Code Online (Sandbox Code Playgroud) 我有一个UICollectionView使用UICollectionViewFlowLayout的自定义子类,使部分标题贴在屏幕的顶部,同时滚动就像UITableView的简单样式.我的代码基于这种方法:
http://blog.radi.ws/post/32905838158/sticky-headers-for-uicollectionview-using
此外,集合视图设置为在用户滚动到底部时加载更多结果.也就是说,当用户到达集合视图的底部时,Web请求会加载更多数据,然后重新加载集合视图,即调用reloadData.
它似乎运行良好,除了我通过运行iOS 7和7.1的TestFlight从beta测试者那里得到一些崩溃报告,他们说当他们快速向下滚动到底部(即触发更多结果加载)时偶尔发生以下情况:
*** -[__NSArrayM objectAtIndex:]: index 28 beyond bounds [0 .. 16]
PRIMARY THREAD THREAD 0
__exceptionPreprocess
objc_exception_throw
-[__NSArrayM objectAtIndex:]
-[UICollectionViewFlowLayout(Internal) _frameForItemAtSection:andRow:usingData:]
-[UICollectionViewFlowLayout layoutAttributesForItemAtIndexPath:usingData:]
-[UICollectionViewFlowLayout layoutAttributesForItemAtIndexPath:]
-[MyCustomCollectionViewFlowLayout layoutAttributesForItemAtIndexPath:]
-[MyCustomCollectionViewFlowLayout layoutAttributesForSupplementaryViewOfKind:atIndexPath:]
-[MyCustomCollectionViewFlowLayout layoutAttributesForElementsInRect:]
-[UICollectionViewData validateLayoutInRect:]_block_invoke
-[UICollectionViewData validateLayoutInRect:]
-[UICollectionView layoutSubviews]
-[UIView(CALayerDelegate) layoutSublayersOfLayer:]
-[CALayer layoutSublayers]
Run Code Online (Sandbox Code Playgroud)
好像当我的自定义流布局代码调用[self.collectionView numberOfItemsInSection:someSection]以获取节的最后一个项的布局属性时,该调用基于新加载的数据返回(例如,在这种情况下,一个节现在有29个项)但是内部的默认流布局仍然使用某种缓存数据(例如,在这种情况下,该部分只有17个项目).不幸的是,我无法自己重现崩溃,即使是经历过它的beta测试人员也无法一致地重现它.
有什么想法吗?