0
我見面的時候我用UICollectionView一個奇怪的錯誤,看看下面的圖片:UICollectionView顯示了兩個線,但我只想要一個行
第三細胞似乎是莫名其妙地顯示第二下,而對於地方第三個單元變成空的。
當我滾動視圖遠,然後返回(或重新載入視圖),原來是正確的:
這裏是我使用的代碼:
#pragma mark - UICollectionView Datasource
- (NSInteger)collectionView:(UICollectionView *)view numberOfItemsInSection:(NSInteger)section {
return datas.count;
}
- (NSInteger)numberOfSectionsInCollectionView:(UICollectionView *)collectionView {
return 1;
}
- (UICollectionViewCell *)collectionView:(UICollectionView *)cv cellForItemAtIndexPath: (NSIndexPath *)indexPath {
static NSString *cellId = @"floorCell";
FloorCollectionViewCell *cell = [cv dequeueReusableCellWithReuseIdentifier:cellId forIndexPath:indexPath];
NSDictionary *dic = [datas objectAtIndex:indexPath.row];
cell.goodImageView.imageURL = [NSURL URLWithString:[dic objectForKey:@"img"]];
cell.goodLabel.text = [dic objectForKey:@"title"];
return cell;
}
而我使用的佈局是UICollectionViewFlowLayout,佈局代表如下:
#pragma mark – UICollectionViewDelegateFlowLayout
- (CGSize)collectionView:(UICollectionView *)collectionView layout:(UICollectionViewLayout *)collectionViewLayout sizeForItemAtIndexPath:(NSIndexPath *)indexPath
{
CGSize retval = CGSizeMake(172/2.0, [FloorCollectionViewCell getHeight]);
return retval;
}
- (UIEdgeInsets)collectionView:(UICollectionView *)collectionView layout:(UICollectionViewLayout*)collectionViewLayout insetForSectionAtIndex:(NSInteger)section {
return UIEdgeInsetsMake(0, 0, 0, 0);
}
我想知道原因以及如何解決問題?謝謝!
給init的CollectionView代碼:
UICollectionViewFlowLayout *layout = [[UICollectionViewFlowLayout alloc] init];
layout.scrollDirection = UICollectionViewScrollDirectionHorizontal;
collectionView = [[UICollectionView alloc] initWithFrame:CGRectMake(50, 0, 278, bottomView.frame.size.height) collectionViewLayout:layout];
collectionView.backgroundColor = [UIColor clearColor];
[collectionView registerClass:[FloorCollectionViewCell class] forCellWithReuseIdentifier:@"floorCell"];
collectionView.dataSource = self;
collectionView.delegate = self;
[bottomView addSubview:collectionView];
PS:我想我已經解決了通過改變問題的CollectionView的高度是電池,而不是整個灰色的高度剛好高度上海華。但我仍然不知道原因,灰色superView的高度不夠大,不足以容納兩個單元!
,改變代碼:
collectionView = [[UICollectionView alloc] initWithFrame:CGRectMake(50, 0, 278, [FloorCollectionViewCell getHeight]) collectionViewLayout:layout];
請發佈您已初始化集合的代碼(可能在您的viewdidload中)。 –
@Thapa我附上了代碼 – itenyh