2

When I build my project the screen just appears white in the simulator (where a collection view is black) and none of the cells that I want to appear either of course.

I am using xcode6-beta6 and this is for ios (ipad).

So I am trying to populate a collection view with labels. In my story board I have the view controller with just the collection view inside it with one cell which I placed a label inside. I know I have the identifier and tag properly set in the story board and my viewcontroller code. I know I also have my dataSource and delegate outlets properly set up. I have been following this tutorial https://www.youtube.com/watch?v=jiiN9oFH3vE however it is in xcode 5 and objective-c so I don't know if there is something that is completely different in swift/xcode6 that I am not doing.

Here is my code

import UIKit

class FirstViewController: UIViewController,UICollectionViewDelegateFlowLayout, UICollectionViewDataSource {

    var testArray:[String]!

override func viewDidLoad() {
    super.viewDidLoad()
    testArray = ["hello","world","hola","mmg","me","la"]
    // Do any additional setup after loading the view, typically from a nib.
}

override func didReceiveMemoryWarning() {
    super.didReceiveMemoryWarning()
    // Dispose of any resources that can be recreated.
}

func numberOfSectionsInCollectionView(collectionView: UICollectionView!) -> Int{
    return 1
}


func collectionView(collectionView: UICollectionView!, numberOfItemsInSection section: Int) -> Int{
        return testArray.count
}
func collectionView(collectionView: UICollectionView!, cellForItemAtIndexPath indexPath: NSIndexPath!) -> UICollectionViewCell!{
    var cell = collectionView.dequeueReusableCellWithReuseIdentifier("Cell", forIndexPath: indexPath) as FirstCollectionViewCell

    var label = cell.viewWithTag(1) as UILabel
    label.text = testArray[indexPath.row]
    return cell
}



}
boidkan
  • 4,301
  • 4
  • 27
  • 42

1 Answers1

0

Are you setting the UICollectionView Delegate and Data Source as self anywhere? I don't see it in your code and that could be the problem. You probably need it in your viewDidLoad method.

BHendricks
  • 4,181
  • 5
  • 28
  • 56
  • I'm sorry, I do not follow. Set it as self? I have not come across this I don't think in any tutorials. – boidkan Aug 29 '14 at 18:47
  • Something like self.collectionView.delegate = self; – BHendricks Aug 29 '14 at 18:52
  • I get the error "(UICollectionView, numberOfItemsInSection:Int)->Int does not have a member named 'delegate'" I also got a similar error for self.collectionView.dataSource = self – boidkan Aug 29 '14 at 19:07
  • U should be doing this in viewdidload – BHendricks Aug 29 '14 at 19:08
  • Yes, I am. Also, xcode is not providing autofill for me which makes me question is this still a thing for swift? Perhaps a different syntax is needed? I'm googling it. – boidkan Aug 29 '14 at 19:15
  • Do you have an outlet to your collection view? – BHendricks Aug 29 '14 at 19:29
  • No, just made it. `@IBOutlet weak var viewOutlet: UICollectionView!` just above `viewDidLoad()`. Same result when building. And same error for `self.collectionView.delegate = self`. – boidkan Aug 29 '14 at 19:39
  • I set the delegate and datasource with the story board though (ctrl + drag to controller and select outlets). Isn't that enough? – boidkan Aug 29 '14 at 19:44
  • So it'd be viewOutlet.delegate. I'm not sure if u need both but that could be the issue. – BHendricks Aug 29 '14 at 20:19