2

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);
}
Community
  • 1
  • 1
sunny
  • 3,853
  • 5
  • 32
  • 62

1 Answers1

0

@property (nonatomic, strong) UICollectionView *collectionView;

Change weak to strong, or collectionView will be released after assignment, which means collectionView will be nil so that the data source methods would never be called.

Another approach:

Keep collectionView to be weak.

- (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);

    UICollectionView *collectionView = [[UICollectionView alloc] initWithFrame:CGRectMake(0, 0, self.width, self.height) collectionViewLayout:flowLayout];
    collectionView.delegate = self;
    collectionView.dataSource = self;
    [collectionView registerClass:[VideoCollectionViewCell class] forCellWithReuseIdentifier:@"cell"];
    [self.view addSubview:collectionView];
    self.collectionView = collectionView;

    [self.collectionView reloadData];
    [self.collectionView reloadInputViews];
}

See: Creating views programmatically Strong Vs Weak subviews in controller

Community
  • 1
  • 1
user3480295
  • 1,058
  • 15
  • 21
  • 2
    you should not declare the property strong. instead create a variable with a new collection view object, add that to the controllers view and than assign it to the property. [more information](https://www.google.de/search?q=uiview+weak+property&ei=UPGKVfXYIsKosAGGrJaYDw#q=site:stackoverflow.com+uiview+weak+vs+strong+property+programmatically) – vikingosegundo Jun 24 '15 at 18:12