【发布时间】:2014-11-12 04:49:51
【问题描述】:
当我快速滑动时,我的一个UIViewController 和UICollectionView 有时会崩溃。它在let option = self.options[indexPath.row] 中崩溃:
func collectionView(collectionView: UICollectionView, cellForItemAtIndexPath indexPath: NSIndexPath) -> UICollectionViewCell {
let cell= collectionView.dequeueReusableCellWithReuseIdentifier("optionCell", forIndexPath: indexPath) as MyCell
let option = self.options[indexPath.row]
return cell
}
我发现问题是 self.options 有时没有成功创建。我在viewWillAppear 中创建了self.options:
override func viewWillAppear(animated: Bool) {
super.viewWillAppear(animated)
self.options = ...
}
有两种可能:
-
viewWillAppear根本没有被调用 -
viewWillAppear将在cellForItemAtIndexPath之后调用
我是否遗漏了代码中的任何内容?谢谢
【问题讨论】:
-
super.viewWillAppear(true)
-
@SaurabhPrajapati 不,这不对。
-
@SaurabhPrajapati 因为如果
animated是false那么你应该将false传递给super。 -
首先调用 [super viewWillAppear:YES/NO] 并将你自己的东西放在下面。
-
我建议你在 viewdidAppear 中创建 self.options。让我知道它是否有效
标签: ios swift uicollectionview