所以我改变了这一点:终止应用程序由于未捕获的异常 'NSRangeException'
func collectionView(collectionView: UICollectionView, layout collectionViewLayout: UICollectionViewLayout, sizeForItemAtIndexPath indexPath: NSIndexPath) -> CGSize {
return CGSizeMake(view.frame.width/2, 40)
}
这样:
func collectionView(collectionView: UICollectionView, layout collectionViewLayout: UICollectionViewLayout, sizeForItemAtIndexPath indexPath: NSIndexPath) -> CGSize {
let cell = collectionView.dequeueReusableCellWithReuseIdentifier(collectionViewCellId, forIndexPath: indexPath) as! CollectionViewCell
return CGSizeMake(view.frame.width/2, 40)
}
,由于某种原因崩溃。我甚至不会把这个常数用到,但它会让人感到困惑。
完整的错误:终止应用程序由于未捕获的异常 'NSRangeException',原因: '*** - [__ NSArrayM objectAtIndex:]:指数1超出范围[0 .. 0]'
我的目标是使单元格的宽度标签的宽度相同+ 5
cell.label.frame.width + 5
我将完全相同的代码行添加到cellForItemAtIndexPath,它工作得很好 – luke
尽量不要使用dequReusableCell函数。集合视图具有将返回单元格的功能。它的名字是一些如何cellForItemAtIndexPath.u。 – Volodymyr
我不明白你的建议。我已经调用了该函数来确定单元格的外观。我相信我不能使用该函数返回单元格的大小。 – luke