【发布时间】:2015-05-11 21:39:39
【问题描述】:
我正在尝试创建一个代表网格的UICollectionView,并且我想在顶部边缘创建一个在 y 轴上浮动的标尺(浮动标题视图)。
所以,就像在UITableViews 中一样,这个视图基本上应该只是随着屏幕顶部的内容滚动。我希望描述足够清楚。理论讲了这么多,让我们进入实践部分!
这是我现在正在使用的:
- 我注册了
GridRulerView类,它是UICollectionReusableView与集合视图。 - 我为正确种类的补充视图和没有标题的 CGSizeZero 返回正确的大小。
-
我实现了
viewForSupplementaryElementOfKind::func collectionView(collectionView: UICollectionView, viewForSupplementaryElementOfKind kind: String, atIndexPath indexPath: NSIndexPath) -> UICollectionReusableView { var reusableHeaderView : UICollectionReusableView! if kind == GridRulerView.Kind{ reusableHeaderView = collectionView.dequeueReusableSupplementaryViewOfKind(GridRulerView.Kind, withReuseIdentifier: GridRulerView.Kind, forIndexPath: indexPath) as! UICollectionReusableView } return reusableHeaderView }
我有一个自定义的UICollectionViewLayout,其中包含以下内容
layoutAttributesForElementsInRect:方法:
override func layoutAttributesForElementsInRect(rect: CGRect) -> [AnyObject]? {
var attribs = [UICollectionViewLayoutAttributes]()
for (indexPath, attrs) in self.attributes{
if CGRectIntersectsRect(rect, attrs.frame) {
attribs.append(attrs)
}
}
var headerAttributes = GridViewRulerAttributes(forSupplementaryViewOfKind: GridRulerView.Kind, withIndexPath: NSIndexPath(forItem: 0, inSection: 0))
headerAttributes.zIndex = 500
headerAttributes.frame = CGRectMake(0, self.collectionView!.contentOffset.y, self.preCalculatedContentSize.width, 80)
attribs.append(headerAttributes)
return attribs
}
当我启动应用程序时,一切看起来都很棒,正是我想要的样子。 但是一旦我滚动 collectionView 超出它的界限(collectionView 的 contentOffset = (-0.5,-0.5)
*** -[UICollectionViewData validateLayoutInRect:] 中的断言失败,/SourceCache/UIKit/UIKit-3318.16.25/UICollectionViewData.m:426
有趣的是,如果我将 header view 属性的 frame 的 y 属性设置为 0 而不是变量 contentOffset.y(变为负数),一切正常。
有人知道为什么会这样吗?
编辑
这里有一个更丰富的错误信息:
*** 由于未捕获的异常“NSInternalInconsistencyException”而终止应用程序,原因:“索引路径 ({length = 2, path = 0 - 0}) 处的补充项目的布局属性已从索引路径更改为: ({length = 2) , 路径 = 0 - 0});元素种类:(GridRulerViewKind);帧 = (0 0; 425.227 24); zIndex = 500;索引路径:( {length = 2, path = 0 - 0});元素种类:(GridRulerViewKind);帧 = (0 -5; 425.227 24); zIndex = 500;不使布局无效'
【问题讨论】:
-
我相信在
*** Assertion...下方的几行中有更多关于这次崩溃的信息。检查他们,他们可以给你一个提示。例如应该有类似:... *** Terminating app due to uncaught exception ... reason: ...
标签: ios swift uicollectionview uicollectionviewlayout