I've just got a standard UIViewController
subclass with a UICollectionView
. That UICollection
view not calling its datasource (or delegate) methods, but I can't see which piece is missing.
Here's the interface for my UIViewController class that presents the UICollectionView:
@interface PastViewController () <UICollectionViewDataSource, UICollectionViewDelegate, UICollectionViewDelegateFlowLayout>
...
@property (nonatomic, weak) UICollectionView *collectionView;
@end
Here is my viewDidLoad (almost) in its entirety:
- (void)viewDidLoad {
[super viewDidLoad];
UICollectionViewFlowLayout* flowLayout = [[UICollectionViewFlowLayout alloc]init];
flowLayout.itemSize = CGSizeMake(100, 100);
[flowLayout setScrollDirection:UICollectionViewScrollDirectionHorizontal];
//self.complaints is used to generate the collection view cells
//the log always prints out 30 complaints
self.complaints = [self.complaintDatabase getComplaints ];
NSLog(@"got a total OF %d complaints", self.complaints.count);
self.collectionView = [[UICollectionView alloc] initWithFrame:CGRectMake(0, 0, self.width, self.height) collectionViewLayout:flowLayout];
self.collectionView.delegate = self;
self.collectionView.dataSource = self;
[self.collectionView registerClass:[VideoCollectionViewCell class] forCellWithReuseIdentifier:@"cell"];
[self.view addSubview:self.collectionView];
[self.collectionView reloadData];
[self.collectionView reloadInputViews];
}
I'll also post the datasource, delegate, and delegate flow layout protocols at the end. There are no other subviews added to the view controller, and I have set the delegate and declared the delegate protocol, so the following SO posts don't apply:
What am I missing? Why would the data source methods (and delegate methods) never be called?
Here are the methods implemented for the various protocols:
#pragma mark - UICollectionViewDataSource
- (NSInteger)collectionView:(UICollectionView *)collectionView numberOfItemsInSection:(NSInteger)section{
NSLog(@"should be seeing %d collection view cells", self.complaints.count);
return self.complaints.count;
}
- (NSInteger)numberOfSectionsInCollectionView:(UICollectionView *)collectionView{
NSLog(@"QUERYING NUMBER OF SECTIONS IN COLLECTION VIEW");
return 1;
}
- (UICollectionViewCell *)collectionView:(UICollectionView *)collectionView cellForItemAtIndexPath:(NSIndexPath *)indexPath{
VideoCollectionViewCell *cell = [[VideoCollectionViewCell alloc] initWithCoder:@"cell"];
cell.complaint = self.complaints[indexPath.row];
return cell;
}
#pragma mark - UICollectionViewDelegate
- (void)collectionView:(UICollectionView *)collectionView didSelectItemAtIndexPath:(NSIndexPath *)indexPath{
NSLog(@"calling didselctitem at path");
}
- (void)collectionView:(UICollectionView *)collectionView didDeselectItemAtIndexPath:(NSIndexPath *)indexPath{
NSLog(@"diddeselectitem");
}
#pragma mar - UICollectionViewFlowDelegateLayout
- (CGSize)collectionView:(UICollectionView *)collectionView layout:(UICollectionViewLayout *)collectionViewLayout sizeForItemAtIndexPath:(NSIndexPath *)indexPath{
return CGSizeMake(100, 100);
}
- (UIEdgeInsets)collectionView:(UICollectionView *)collectionView layout:(UICollectionViewLayout *)collectionViewLayout insetForSectionAtIndex:(NSInteger)section{
return UIEdgeInsetsMake(50, 20, 50, 20);
}