Skip to content

Commit

Permalink
fixed crash when no items are specified
Browse files Browse the repository at this point in the history
  • Loading branch information
akkyie committed May 14, 2015
1 parent f4fb251 commit 3e557bd
Showing 1 changed file with 13 additions and 7 deletions.
20 changes: 13 additions & 7 deletions AKPickerView/AKPickerView.swift
Original file line number Diff line number Diff line change
Expand Up @@ -173,9 +173,11 @@ private class AKCollectionViewLayout: UICollectionViewFlowLayout {
return super.layoutAttributesForElementsInRect(rect)
case .Wheel:
var attributes = [AnyObject]()
for i in 0 ..< self.collectionView!.numberOfItemsInSection(0) {
let indexPath = NSIndexPath(forItem: i, inSection: 0)
attributes.append(self.layoutAttributesForItemAtIndexPath(indexPath))
if self.collectionView!.numberOfSections() > 0 {
for i in 0 ..< self.collectionView!.numberOfItemsInSection(0) {
let indexPath = NSIndexPath(forItem: i, inSection: 0)
attributes.append(self.layoutAttributesForItemAtIndexPath(indexPath))
}
}
return attributes
}
Expand Down Expand Up @@ -346,8 +348,10 @@ public class AKPickerView: UIView, UICollectionViewDataSource, UICollectionViewD

public override func layoutSubviews() {
super.layoutSubviews()
self.collectionView.collectionViewLayout = self.collectionViewLayout
self.scrollToItem(self.selectedItem, animated: false)
if self.dataSource != nil && self.dataSource!.numberOfItemsInPickerView(self) > 0 {
self.collectionView.collectionViewLayout = self.collectionViewLayout
self.scrollToItem(self.selectedItem, animated: false)
}
self.collectionView.layer.mask?.frame = self.collectionView.bounds
}

Expand Down Expand Up @@ -411,7 +415,9 @@ public class AKPickerView: UIView, UICollectionViewDataSource, UICollectionViewD
self.invalidateIntrinsicContentSize()
self.collectionView.collectionViewLayout.invalidateLayout()
self.collectionView.reloadData()
self.selectItem(self.selectedItem, animated: false, notifySelection: false)
if self.dataSource != nil && self.dataSource!.numberOfItemsInPickerView(self) > 0 {
self.selectItem(self.selectedItem, animated: false, notifySelection: false)
}
}

/**
Expand Down Expand Up @@ -497,7 +503,7 @@ public class AKPickerView: UIView, UICollectionViewDataSource, UICollectionViewD

// MARK: UICollectionViewDataSource
public func numberOfSectionsInCollectionView(collectionView: UICollectionView) -> Int {
return 1
return self.dataSource != nil && self.dataSource!.numberOfItemsInPickerView(self) > 0 ? 1 : 0
}

public func collectionView(collectionView: UICollectionView, numberOfItemsInSection section: Int) -> Int {
Expand Down

0 comments on commit 3e557bd

Please sign in to comment.