Reputation: 4716
I have a UIViewController
which contains a CollectionView
but as output goes all white
In GridViewController.h
#import <UIKit/UIKit.h>
@interface GridViewController : UIViewController <UICollectionViewDataSource,
UICollectionViewDelegate>{
}
@property (nonatomic, strong)UIImageView *imageHeader;
@property (nonatomic, strong)UIButton * buttonHome;
@property (nonatomic, strong)UILabel * labelTitle;
@property (nonatomic, strong)UICollectionView * collectionView;
//....
@end
In GridViewController.m
- (void)viewDidLoad
{
//....
[self.collectionView registerClass:[UICollectionView class]
forCellWithReuseIdentifier:@"Cell"];
NSLog(@"%@", self.collectionView);//here (null), why?
self.collectionView.delegate=self;
self.collectionView.dataSource=self;
//...
}
- (NSInteger)numberOfSectionsInCollectionView: (UICollectionView *)collectionView
{
return 1;
}
- (NSInteger)collectionView:(UICollectionView *)view numberOfItemsInSection:
(NSInteger)section;
{
return 32;
}
- (UICollectionViewCell *)collectionView:(UICollectionView *)cv cellForItemAtIndexPath:
(NSIndexPath *)indexPath{
NSString *kCellID = @"cellID";
CollectionViewCellCustom *cell = [cv dequeueReusableCellWithReuseIdentifier:kCellID forIndexPath:indexPath];
cell.imageView.backgroundColor =[UIColor greenColor];
return cell;
}
Upvotes: 0
Views: 8004
Reputation: 42977
I did not see any outlet in your code. So i'am assuming you are try to create it programmatically. For that you should do
UICollectionViewFlowLayout *layout= [[UICollectionViewFlowLayout alloc]init];
self.collectionView = [[UICollectionView alloc]initWithFrame:self.view.bounds collectionViewLayout:layout];
[self.view addSubView:self.collectionView];
[self.collectionView registerClass:[UICollectionViewCell class]
forCellWithReuseIdentifier:@"Cell"];
self.collectionView.delegate=self;
self.collectionView.dataSource=self;
In your code i can see you doing registerClass:[UICollectionView class]
which is wrong registerClass:[UICollectionViewCell class]
is right.
Change
[self.collectionView registerClass:[UICollectionView class]forCellWithReuseIdentifier:@"Cell"];
to
[self.collectionView registerClass:[UICollectionViewCell class]forCellWithReuseIdentifier:@"Cell"];
One more mistake you are using different cell id is for registering and dequeing. make it as same. Register the cell with id Cell and trying to deque using cellID
Upvotes: 4