2016-06-14 61 views
0

当我在代码中使用FlowLayout对象并在CollectionView对象上调用reloadData()时,我的应用程序崩溃。当我使用标准的FlowLayout(来自XCode的设置)时,我没有这样的例外。UICollectionView和reloadData的FlowLayout()

这是我如何使用FlowLayout根据设备宽度计算部分的插图。

override func viewDidLayoutSubviews() { 
    super.viewDidLayoutSubviews() 

    let layout = UICollectionViewFlowLayout() 
    layout.itemSize = CGSize(width: itemWidth, height: itemWidth) 
    layout.minimumInteritemSpacing = minItemSpacing 
    layout.minimumLineSpacing = minItemSpacing 

    layout.headerReferenceSize = CGSize(width: 0, height: headerHeight) 

    let containerWidth = collectionView.bounds.width 
    if (containerWidth < 350) { 
     inset = 2 
    } 
    else if (containerWidth >= 350 && containerWidth < 400) { 
     inset = 10 
    } 
    else { 
     inset = 14 
    } 
    layout.sectionInset = UIEdgeInsets(top: minItemSpacing, left: inset, bottom: minItemSpacing, right: inset) 

    collectionView.collectionViewLayout = layout 
} 

与这样的代码的应用程序启动正常,但是当我从另一个视图控制器到主要地方去的CollectionView使用和collectionView.reloadData()被调用有以下例外

@IBAction func saveMyObjects(segue:UIStoryboardSegue) { 
    if let myObjectsViewController = segue.sourceViewController as? MyObjectsViewController { 
     MyObjects.Objects.removeAll() 
     MyObjects.Objects.appendContentsOf(myObjectsViewController.MyObjects) 
     collectionView.reloadData() 
    } 
} 

应用崩溃。

*终止应用程序由于未捕获的异常 'NSRangeException',原因: '* - [__ NSArrayM objectAtIndex:]:索引0超出界限为空数组' 的libC++ abi.dylib:与类型的未捕获的异常终止NSException

如果我评论

// collectionView.collectionViewLayout = layout 

线使用在Xcode值,应用程序的工作原理没有例外。

附加代码:

func collectionView(collectionView: UICollectionView, cellForItemAtIndexPath indexPath: NSIndexPath) -> UICollectionViewCell { 

    let cell = collectionView.dequeueReusableCellWithReuseIdentifier(reuseIdentifier, forIndexPath: indexPath) as! ObjectCollectionViewCell 
    // Configure the cell 
    cell.imageView.image = UIImage(named: Groups[indexPath.section].Objects[indexPath.row].Image) 
    cell.label.text = Groups[indexPath.section].Objects[indexPath.row].Name 
    if (Groups[indexPath.section].Objects[indexPath.row].Image != "") { 
     cell.label.hidden = true 
     cell.imageView.hidden = false 
    } 
    else { 
     cell.imageView.hidden = true 
     cell.label.hidden = false 
    } 
    return cell 
} 
+0

你可以发布你的crash stackTrace吗?看看会发生什么? –

+0

你好,***由于未捕获异常'NSRangeException',原因:'*** - [__ NSArrayM objectAtIndex:]:索引0超出空数组界限' ***第一次抛出调用堆栈: (0x182afedb0 0x182163f80 0x1829dedfc 0x1884bcf80 0x1884b95e4 0x1884ba254 0x187de5394 0x187de4870 0x100112048 0x1001121c8 0x187c50224 0x1855e2994 0x1855dd5d0 0x1855dd490 0x1855dcac0 0x1855dc820 0x1855d5de4 0x182ab4728 0x182ab24cc 0x1829dcc70 0x187cc394c 0x187cbe088 0x10013d8fc 0x18257a8b8) 的libC++ abi.dylib:与类型NSException – Anton

+0

@ReinierMelian的XCode的未捕获的异常终止示出了在 “14开始” 的问题,如果我点击它,出现以下信息libdyld.dylib'start: 0x18257a8b4 <+0>:nop 0x18257a8b8 <+4>:bl 0x1825b534c;退出 0x18257a8bc <+8>:brk#0x3 – Anton

回答

0

后,审查你的代码,所以很多时候,我觉得你的问题是,你应该从viewDidLayoutSubviews()删除代码和执行现有的UICollectionViewDelegateFlowLayout的方法,你需要什么,

optional public func collectionView(collectionView: UICollectionView, layout collectionViewLayout: UICollectionViewLayout, sizeForItemAtIndexPath indexPath: NSIndexPath) -> CGSize 

optional public func collectionView(collectionView: UICollectionView, layout collectionViewLayout: UICollectionViewLayout, minimumLineSpacingForSectionAtIndex section: Int) -> CGFloat 

optional public func collectionView(collectionView: UICollectionView, layout collectionViewLayout: UICollectionViewLayout, minimumInteritemSpacingForSectionAtIndex section: Int) -> CGFloat 

optional public func collectionView(collectionView: UICollectionView, layout collectionViewLayout: UICollectionViewLayout, referenceSizeForHeaderInSection section: Int) -> CGSize 

optional public func collectionView(collectionView: UICollectionView, layout collectionViewLayout: UICollectionViewLayout, insetForSectionAtIndex section: Int) -> UIEdgeInsets 

我想,如果实施该方法,并从viewDidLayoutSubviews()删除你的代码的代码将工作,因为你需要,强烈建议不要做你的方法),我希望这可以帮助你