UICollectionView: I am doing it wrong. I just don’t know how.
My setting
I am running this on an iPhone 4S with iOS 6.0.1.
My goal
I have a table view in which one section is dedicated to images: 
When the user selects the "Add Image ..." cell, they are prompted to select an image from their photo library or take a new one using the camera. This part of the application is working fine.
When the user first adds an image, the “No Image” label will be removed from the second cell of the table, and the UICollectionView program code will be added in its place. This part also works fine.
It is assumed that in the collection view the images that they added will be displayed here, too, where I ran into difficulties. (I know that I will have to jump over some hoops to make the table view cell expand itself as the number of images increases. I'm not so far away.)
When I try to insert an item into the collection view, it throws an exception. More on this later.
My code
I have a UITableViewController that is responsible for presenting the table, also acting as the delegate of the collection view and data source. Here is the relevant code (I skipped controller bits that I believe are not related to this problem, including lines in methods such as -viewDidLoad . I also skipped most of the image acquisition code, since I don't consider it relevant):
Summarizing:
- The collection view is created and configured in the
-viewDidLoad method - UIImagePickerDelegate method that accepts selected image images
-addImage:toCollectionView - ... which creates a new image representation for storing the image and adds it to the data array and data set. This is the string that throws the exception.
- The datasource UICollectionView data methods rely on an array of imageViews.
An exception
The application terminated due to the unannounced exception "NSInternalInconsistencyException", reason: "Invalid update: invalid number of elements in section 0. The number of elements contained in an existing section after updating (1) must be equal to the number of elements contained in this section before updating (1 ) plus or minus the number of elements inserted or deleted from this section (1 inserted, 0 deleted) and plus or minus the number of elements moved to or from this section (0 moved to, 0 displayed).
If I understand this correctly, what this tells me is that the collection view (brand new!) Believes that it was created with a single element. So, I added the -addImage:toCollectionView to test this theory:
NSLog(@"%d", [cv numberOfItemsInSection:0]);
With this line there an exception never throws! The call to -numberOfItemsInSection: should make the collection view access its data source and understand that it has no elements. Or something. I guess here. But, well, be that as it may: in the collection view still no elements are displayed, so I'm still doing something wrong, and I don't know what.
In custody
- I get a weird exception when I try to add an item to a recently modified and pasted collection view ... unless I call
-numberOfItemsInSection: before inserting. - Even if I manage to get around the exception with a shadow workaround, the items are still not showing up in the collection view.
Sorry for the novel issue. Any ideas?