我的UICollectionView不调用其数据源方法?

时间:2015-06-24 16:24:29

标签: ios objective-c uicollectionview

我刚刚获得了一个标准UIViewController子类UICollectionView。那个UICollection视图没有调用它的数据源(或委托)方法,但是我看不到哪一块丢失了。

这是我的UIViewController类的界面,它呈现了UICollectionView:

@interface PastViewController ()  <UICollectionViewDataSource, UICollectionViewDelegate, UICollectionViewDelegateFlowLayout>
...
@property (nonatomic, weak) UICollectionView *collectionView;
@end

这是我的viewDidLoad(几乎)的全部内容:

- (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];
}

我还会在最后发布数据源,委托和委派流程布局协议。没有其他子视图添加到视图控制器,我已设置委托并声明了委托协议,因此以下SO帖子不适用:

我错过了什么?为什么永远不会调用数据源方法(和委托方法)?

以下是为各种协议实施的方法:

#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);
}

1 个答案:

答案 0 :(得分:0)

@property (nonatomic, strong) UICollectionView *collectionView;

weak更改为strong,或collectionView将在分配后释放,这意味着collectionView将为nil,以便数据源方法永远不会被称为。

另一种方法:

collectionView保持为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];
}

请参阅:Creating views programmatically Strong Vs Weak subviews in controller