'UICollectionView : Cells do not appear/populate
I need some help with a uicollectionview that I've been trying to implement in Objective-C/xcode: I am unable to have the cells appear/populate the collectionview (CV). Currently I am using the interface builder, and a custom class for cells. The CV is to be a wide strip that is a single row of square image cells, that can be scrolled/swept through horizontally. My attempts at implementation result in the wide strip but it never populates with cells. I have explored other stackoverflow threads similar to this issue, where they discuss the subclassing of cell but I was unable to make it work. Have also tried programmatic implementation and had the same issue. Would really appreciate any insight as I have been stuck at this point for days. Below the code I am using, are screenshots of interface builder showing different settings... some I know are useful to include and I apologize if I have shared anything useless.
from ViewController.h
@interface ViewController : UIViewController <UICollectionViewDelegate,UICollectionViewDataSource>
@property (weak, nonatomic) NSMutableArray *medCollectionCells;
from ViewController.m
#import "CoolCell.h"
@property (weak, nonatomic) IBOutlet UICollectionView *medCollection;
- (void)viewDidLoad
{
_medCollectionCells = [NSMutableArray arrayWithObjects:@"1st cell",@"2nd cell",@"3rd cell",@"4th cell",@"5th cell",@"6th cell",@"7th cell",@"8th cell",@"5th cell", nil];
self.medCollection.delegate = self;
self.medCollection.dataSource = self;
}
// collection view delegate methods
- (NSInteger)collectionView:(UICollectionView *)collectionView numberOfItemsInSection:(NSInteger)section
{
return [_medCollectionCells count];
}
- (UICollectionViewCell *)collectionView:(UICollectionView *)collectionView cellForItemAtIndexPath:(NSIndexPath *)indexPath
{
CoolCell *cell = (CoolCell *)[_medCollection dequeueReusableCellWithReuseIdentifier:@"MyCustomCell" forIndexPath:indexPath];
cell.backgroundColor = [UIColor blackColor];
return cell;
}
CoolCell.h:
#import <UIKit/UIKit.h>
@interface CoolCell : UICollectionViewCell
@property (weak, nonatomic) IBOutlet UILabel *customLabel;
@property (weak, nonatomic) IBOutlet UIImageView *customImage;
@end
CoolCell.m:
#import "CoolCell.h"
@implementation CoolCell
@end
Screen Shots
Grateful for any assistance/insight. Thank you.
Solution 1:[1]
Seems like the delegate and dataSource properties are connected to a View object in Interface Builder. Maybe remove that since you are setting those properties in code.
Sources
This article follows the attribution requirements of Stack Overflow and is licensed under CC BY-SA 3.0.
Source: Stack Overflow
| Solution | Source |
|---|---|
| Solution 1 | Farid Rahmani |
