Swift doesn't see a variable
I want to pass a variable to
func collectionView(collectionView: UICollectionView, numberOfItemsInSection section: Int) -> Int {
return 6 // <-- HERE
}
of
func collectionView(collectionView: UICollectionView, cellForItemAtIndexPath indexPath: NSIndexPath) -> UICollectionViewCell { ... }
for this i do the following:
class GalleryController: UIViewController {
var galleryCount = 0 as Int
}
and then in
func collectionView(collectionView: UICollectionView, cellForItemAtIndexPath indexPath: NSIndexPath) -> UICollectionViewCell {
var gallery = myJSON["result"][choosenRow]["gallery"]
galleryCount = gallery.count
}
I override my galleryCount variable and when I want to use it in
func collectionView(collectionView: UICollectionView, numberOfItemsInSection section: Int) -> Int {
return galleryCount // <-- HERE
}
I get an error: error: <EXPR>:1:1: error: use of unresolved identifier 'galleryCount' galleryCount
Why? I don't understand this error. Can anyone help me with this?
source to share
Try to define a variable with no value
class GalleryController: UIViewController {
var galleryCount:Int = 0
}
And initialize its value to viewDidLoad
because the first method that is called in the collection delegate is numberOfItemsInSection
notcellForItemAtIndexPath
EDIT
The first method is called numberOfItemsInSection
so it galleryCount
will remain 0 and yours is cellForItemAtIndexPath
never called.
If you want to use galleryCount
, then do it in viewDidLoad
.
source to share