【发布时间】:2015-09-10 23:21:21
【问题描述】:
我刚刚有了一个标准的UIViewController 子类和一个UICollectionView。 UICollection 视图没有调用它的数据源(或委托)方法,但我看不到哪一块丢失了。
这是呈现 UICollectionView 的 UIViewController 类的界面:
@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);
}
【问题讨论】:
标签: ios objective-c uicollectionview