Reputation: 947
My app crashes when I use FlowLayout object in the code and call reloadData() on CollectionView object. I do not have such exception when I use standard FlowLayout (settings from XCode).
This is how I use FlowLayout to calculate section's inset depending on device width.
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
}
with such code app starts normally, but when I go from another view controller to main where CollectionView is used and collectionView.reloadData() is called
@IBAction func saveMyObjects(segue:UIStoryboardSegue) {
if let myObjectsViewController = segue.sourceViewController as? MyObjectsViewController {
MyObjects.Objects.removeAll()
MyObjects.Objects.appendContentsOf(myObjectsViewController.MyObjects)
collectionView.reloadData()
}
}
app crashes with the following exception.
* Terminating app due to uncaught exception 'NSRangeException', reason: '* -[__NSArrayM objectAtIndex:]: index 0 beyond bounds for empty array' libc++abi.dylib: terminating with uncaught exception of type NSException
If I comment
// collectionView.collectionViewLayout = layout
line to use values from XCode, app works without exception.
Additional code:
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
}
Upvotes: 0
Views: 1391
Reputation: 20804
after to review your code so many times, I think that your problem is that you should remove the code from viewDidLayoutSubviews()
and implement the methods existing on UICollectionViewDelegateFlowLayout
to do what you need,
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
I think if you implement this methods and remove your code from viewDidLayoutSubviews()
your code will work as you need, Is highly discouraged to do that with your approach ;), I hope this helps you
Upvotes: 0