UICollectionView单元格宽度问题

时间:2016-09-30 15:47:05

标签: ios objective-c uicollectionview uicollectionviewcell

我有UICollectionView自定义高度和宽度但是当我启动应用程序时,所有最初对屏幕可见的单元格都是根据我的高度和宽度完美设置的,但是当我向下滚动时它减少了单元格宽度。 Here is the screenshot

以下是设置高度和宽度,间距等的方法。

- (CGFloat)collectionView:(UICollectionView *)collectionView layout:(UICollectionViewLayout*)collectionViewLayout minimumInteritemSpacingForSectionAtIndex:(NSInteger)section
{
    return 5.0;
}

- (CGFloat)collectionView:(UICollectionView *)collectionView layout:(UICollectionViewLayout*)collectionViewLayout minimumLineSpacingForSectionAtIndex:(NSInteger)section
{
    return 5.0f;
}

- (CGSize)collectionView:(UICollectionView *)collectionView layout:(UICollectionViewLayout*)collectionViewLayout sizeForItemAtIndexPath:(NSIndexPath *)indexPath
{
    [self.collectionView setNeedsLayout];
    [self.collectionView layoutIfNeeded];
    CGFloat width = self.collectionView.frame.size.width;
    return CGSizeMake((width/2)-5, 220);
}


-(NSInteger)numberOfSectionsInCollectionView:(UICollectionView *)collectionView{
    return [listOfRetailers_ count];
}
-(void)collectionView:(UICollectionView *)collectionView willDisplayCell:(UICollectionViewCell *)cell forItemAtIndexPath:(NSIndexPath *)indexPath{
    BDRetailerCollectionViewCell *newCell = (BDRetailerCollectionViewCell*)cell;
    UIFont *font = newCell.brandName.font;
    [newCell.brandName setFont:[font fontWithSize:12]];
    [newCell setNeedsLayout];
    [newCell layoutIfNeeded];

}

1 个答案:

答案 0 :(得分:0)

从此方法中删除此行

(CGSize)collectionView:(UICollectionView *)collectionView layout:(UICollectionViewLayout*)collectionViewLayout sizeForItemAtIndexPath:(NSIndexPath *)indexPath
[self.collectionView setNeedsLayout];
[self.collectionView layoutIfNeeded];