即使我正在设置委托(delegate)和数据源,也永远不会调用数据源方法。
我有一个添加 subview 的 ViewController:
EVPhotoCollectionViewController *pc = [[EVPhotoCollectionViewController alloc] initWithNibName:@"EVPhotoCollectionViewController" bundle:nil];
self.damagePhotosView = pc.view;
在 EVPhotoCollectionViewController 内部,我在 xib 中连接了委托(delegate)和数据源,但也通过如下代码:
- (void)viewDidLoad
{
[super viewDidLoad];
// Do any additional setup after loading the view from its nib.
self.collectionView.delegate = self;
self.collectionView.dataSource = self;
[self.collectionView reloadData];
}
没有调用任何数据源方法。我已验证 self.collectionView 在调用 reloadData 时不为 null。
谢谢!
最佳答案
我认为这里有几处错误——查看 Controller 包含有一些复杂性,您可能需要继续阅读。
首先,您没有将 EVPhotoCollectionViewController View 添加为 vc 的 subview ,例如:
[self addSubView:pc.view];
此外,您没有为 EVPhotoCollectionViewController 设置框架,因此根据它的实现方式,它可能无法以正确的大小/位置显示。
最后,看起来您没有在任何地方保留 EVPhotoCollectionViewController。它的 View 将由 View 层次结构保留,但看起来 EVPhotoCollectionViewController 的实例一旦创建它的函数超出范围就会被释放。
查看 Controller 包含:How does View Controller Containment work in iOS 5?
关于ios - 未调用 UICollectionView 数据源,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/17660443/