今天遇到一個關於UICollectionView的問題。
在xib中拖入UICollectionView,設置delegate和dataSource,都配置好了,但是發現,
部分數據源方法不被調用。
#pragma mark UICollectionView delegate and datasource
- (NSInteger)numberOfSectionsInCollectionView:(UICollectionView *)collectionView {
return 1;
}
- (NSInteger)collectionView:(UICollectionView *)collectionView numberOfItemsInSection:(NSInteger)section {
return 17;
}
//以上兩個方法是被正常調用的
//但是cellForItemAtIndexPath方法不被調用
- (UICollectionViewCell *)collectionView:(UICollectionView *)collectionView cellForItemAtIndexPath:(NSIndexPath *)indexPath{
static NSString *identifier = @"WHHostCollectionCell";
WHHostCollectionCell *cell = [collectionView dequeueReusableCellWithReuseIdentifier:identifier forIndexPath:indexPath];
return cell;
}
- (void)collectionView:(UICollectionView *)collectionView didSelectItemAtIndexPath:(NSIndexPath *)indexPath{
}
發現其他方法正常,只有cellForItemAtIndexPath不被調用。查找了好長時間,誤打誤撞,發現將代碼中的
layout.scrollDirection = UICollectionViewScrollDirectionHorizontal;注釋掉就可以正常回調了。
至於原因,還不知為什么,按理該行代碼不應該影響方法的回調,所以還未確定問題,持續查找原因。