PSCollectionView的委托不会触发

我遵循PSCollectionView在Github上的用法。

这是我的代码:

- (void)viewDidLoad { self.collectionView = [[PSCollectionView alloc] init]; self.collectionView.collectionViewDataSource = self; self.collectionView.collectionViewDelegate = self; self.collectionView.numColsPortrait = 2; [self.view addSubview:self.collectionView]; [self.collectionView reloadData]; // If i don't call this my console will be empty. } - (NSInteger)numberOfRowsInCollectionView:(PSCollectionView *)collectionView { NSLog(@"num rows"); return 2; } - (PSCollectionViewCell *)collectionView:(PSCollectionView *)collectionView cellForRowAtIndex:(NSInteger)index { NSLog(@"cell for row"); return nil; } - (CGFloat)collectionView:(PSCollectionView *)collectionView heightForRowAtIndex:(NSInteger)index { NSLog(@"height for row"); return 0.0; } 

我的控制台是空的。 但是当我调用[collectionView reloadData]时,结果如下:

 num rows height for row height for row num rows 

我不知道为什么“行的单元格”不打印出来。

任何暗示或build议? 谢谢。

这是因为你有错字,它不是ViewDidLoad()它是viewDidLoad() ,所以你的ViewDidLoad()通常不会被调用。 所以,你无法正确设置你的委托和数据源。

尝试添加

[self.collectionView registerClass:[UICollectionViewCell class] forCellWithReuseIdentifier:@"cell"];

在viewDidLoad的末尾。

通常你需要告诉你的collection查看使用的Cell / Views。

最后我可以做到这一点!

您必须设置PSCollectionView实例的框架。