1
votes

I'm new to iOS programming. Im trying to create use a UICollectionView to display a grid of images. I followed this tutorial: http://www.youtube.com/watch?v=ijAzOIRhhzA and executed the following steps:

  1. Placed the UICollectionView in Xcode into my view
  2. Class not set to anything
  3. Created a outlet property for this UICollectionView in my view controller and declared the view controller as the datasource and delegate of the UICollection view as follows: @interface appViewController : ViewController < UICollectionViewDelegate,UICollectionViewDataSource> @end
  4. Set the layout property as "flow" and the scroll direction as horizontal
  5. Created a xib (view) deleted the default view and placed a UICollectionViewCell and filled it with a UIImage (set the identifier of the UICollectionViewCell to "appCell"
  6. Created a class (subclassing UICollectionViewCell) and created an outlet for the UIImage
  7. Class, XIB all named the same - appNameCellCollectionViewCell (.h, .m, .xib)
  8. In the viewDidLoad of the viewController put the following code: [self.outletUICollectionView registerNib:[UINib nibWithNibName:@"appCellCollectionViewCell" bundle:nil] forCellWithReuseIdentifier:@"appCell"];
  9. Implemented the numberOfItemsInSection, numberOfSectionsInCollectionView, cellForItemAtIndexPath in the viewController as follows: -(appCellCollectionViewCell *)collectionView:(UICollectionView *)collectionView cellForItemAtIndexPath:(NSIndexPath *)indexPath{ appCellCollectionViewCell *cell = [collectionView dequeueReusableCellWithReuseIdentifier:@"appCell" forIndexPath:indexPath]; cell.appCellImage.image = [set image]; return cell; }

I know I'm supposed to set the datasource and delegate of the outlet to self in the view controller but I'm not sure where exactly to set that. I tried doing that in the view did load, above and below the step 8 code but no diff. I get a black area instead of the UICollectionView.

I checked that the UIImage is present and everything. I guess I'm missing something please help me !!

Thanks

1

1 Answers

1
votes

The black area is likely due to something not being hooked up between the storyboard and your code.

If you can't figure it out from the storyboard, try setting breakpoints in your code, and examining outlets to see if any are nil.

You can set the delegate and dataSource in code, but it's much cleaner to set everything up via the storyboard. Open the storyboard's view controller, use ⌥⌘6 to open the Connections Inspector, then control-drag from a connection to the view controller (or control) in the storyboard.

Unless you have a specific reason, you may find it much easier to use a UICollectionViewController instead of a UIViewController. It will handle a lot of things for you, and make your code much simpler.

Update:

You're trying to use someone else's code, while being new to iOS. Just looking at the code you shared with me, I can see that objects aren't being named properly, and you're making mistakes like trying to assign one type of object to another type.

You're probably over your head right now trying to deal with keyboard extensions, the cloud, third-party frameworks, and nibs, all at once, and it's not helping you to learn about fundamentals like views, view controllers, using Xcode, and how to connect outlets.

  • I'd really recommend taking a step back and looking at Apple's Getting Started roadmap. This will help you learn Xcode, how to work with storyboards, and how to hookup outlets and delegates.

  • After you've gone through Apple's tutorial, and want to start on your app idea, try to focus on just one particular aspect. Look for a UICollectionViewController tutorial that uses a collection view and a collection view cell in a storyboard. raywenderlich.com has lots of great tutorials. Understand how to load local images in that project. Ignore nibs, keyboard extensions, fetching data from the cloud, etc for now.

  • Once you understand those basics, then work on moving the UICollectionViewCell to a nib, and get your storyboard UICollectionView and UICollectionViewController working with the nib.

Learning and building on the basics will get you to the point where you can understand how to load images from the cloud into your collection view.

What you're asking for help on now in the comments, is why the view controller can't dequeue a cell. It's because the code isn't hooked up properly. This is why I suggest learn the basics, so you know how to debug fundamental stuff like this, understand why it's crashing, and how to fix it.

Learning and developing apps for iOS is enjoyable, but I think you'll be less frustrated if you pick up the basics before trying to tackle a more sophisticated project involving keyboard extensions, and third-party frameworks.