Reputation: 2024
What I'm trying to achieve is a 3x3 table with UICollectionView
. But it just doesn't display any cells when running the application. It does display the background color of the CollectionView
though (which I set to blue) Can someone help me here?
What I have done is create a storyboard
and simply dragged a CollectionViewController
onto it. This controller also brought a nested CollectionView
with it. Then I simply used the settings to make its background blue and to add a cell to it of which I made the background purple (both only for testing). Finally dragged a UILabel
onto it. Now when run it just displays the blue background but no cells even though they are clearly visible in the editor. I've then tried to create a custom UICollectionViewController
, assign the class to the CollectionViewController
instead of the standard class and did the same to the cell (assign a custom UICollectionViewCell
). In the custom UICollectionViewController
I implemented the following methods:
-(NSIntegear)numberOfSectionsInCollectionView:(UICollectionView *)collectionView
{
return 1;
}
-(NSInteger)collectionView:(UICollectionView *)collectionView numberOfItemsInSection:(NSInteger)section
{
return 9;
}
-(UICollectionViewCell *)collectionView:(UICollectionView *)collectionView cellForItemAtIndexPath:(NSIndexPath *)indexPath
{
static NSString *cellIdentifier = @"Cell";
AboutYourGoalsMultiSelectionCell *cell = [collectionView dequeueReusableCellWithReuseIdentifier:cellIdentifier forIndexPath:indexPath];
[[cell goal]setText:@"T"];
return cell;
}
I set the identifier of the cell to "Cell" and made an outlet connection of dataSource and delegate between the CollectionView and the custom UICollectionViewController. Everything compiles fine. So what am I missing? Shouldn't it have displayed something way before doing all these tweaks by simply using the editor?
Upvotes: 19
Views: 20991
Reputation: 99
If you are trying to create just a simple CollectionViewController (not custom i.e. by implementation) and your cells are not displaying when application runs just follow the following steps:-
Now open your created class and edit your methods like this:
override func numberOfSectionsInCollectionView(collectionView: UICollectionView) -> Int {
//#warning Incomplete method implementation -- Return the number of sections
return 1
}
override func collectionView(collectionView: UICollectionView, numberOfItemsInSection section: Int) -> Int {
//#warning Incomplete method implementation -- Return the number of items in the section
return 1
}
Only edit "return" value (which is 0 by default) just simply write the number of cells/items you want to display or you have created (in this case here i only had created one Cell).
[ios] [swift3] [xcode]
Upvotes: 0
Reputation: 47364
See my answer here, in short, you need to delete this line if you are working with a storyboard:
// Register cell classes
[self.collectionView registerClass:[UICollectionViewCell class] forCellWithReuseIdentifier:reuseIdentifier];
Upvotes: 49
Reputation: 2865
A common source of error is that dataSource and delegate are not set in the Outlets
Upvotes: 6
Reputation: 2024
Since I didn't find a direct solution to it I've tried to set up a new project and did the same changes and tweaks and surprisingly, this time, everything worked from the beginning. I then copied the storyboard to my old project, deleted all the old files and replaced it with the files from the new project. Everything is working good so far. I have no idea what the actual error was but I'm happy it's working now. Thanks to everyone who had a look at my question!
Upvotes: 2
Reputation: 3556
You have an error in the first line of your code.
-(NSIntegear)numberOfSectionsInCollectionView:(UICollectionView *)collectionView
NSIntegear
should be NSInteger
.
Not sure if that's the problem though because xcode shouldn't allow you to run your project with this error. I just tried following the exact same steps you mentioned (without the label) but with the above correction and it worked fine for me. Here is a screenshot:
Upvotes: 3