UICollectionViewCell 不按常理流动
The UICollectionViewCell do not flow according to common sense
我设置了我的 UICollectionView 的委托方法:
#pragma mark - collectionView delegate
- (NSInteger)numberOfSectionsInCollectionView:(UICollectionView *)collectionView {
return 1;
}
- (NSInteger)collectionView:(UICollectionView *)collectionView numberOfItemsInSection:(NSInteger)section {
//CGPoint point = self.collectionView.frame.origin;
//CGRect rect = CGRectMake(point.x, point.y, self.collectionView.bounds.size.width, 70*2);
// height
/*_heightOfCollectionView.constant = kHeight_Collection_Cell * 6;
_heightOfContents.constant = collectionView.frame.origin.y + 230 + _heightOfCollectionView.constant;*/
return 12;//_recommendDataArr.count;
}
-(CGSize)collectionView:(UICollectionView *)collectionView layout:(UICollectionViewFlowLayout *)collectionViewLayout sizeForItemAtIndexPath:(NSIndexPath *)indexPath{
float width = (self.view.bounds.size.width / 2);
float height = 89; // 89
return CGSizeMake(width, height);
}
- (CGFloat)collectionView:(UICollectionView *)collectionView layout:(UICollectionViewLayout*)collectionViewLayout minimumLineSpacingForSectionAtIndex:(NSInteger)section
{
return 0;
}
- (UICollectionViewCell *)collectionView:(UICollectionView *)collectionView cellForItemAtIndexPath:(NSIndexPath *)indexPath {
HomeGMCell *cell = [collectionView dequeueReusableCellWithReuseIdentifier:@"HomeGMCell" forIndexPath:indexPath];
if (cell == nil) {
cell = [collectionView dequeueReusableCellWithReuseIdentifier:@"HomeGMCell" forIndexPath:indexPath];
}
return cell;
}
minimumLineSpacingForSectionAtIndex
是 0
:
你看我 return 单元格的宽度是:
(self.view.bounds.size.width / 2) // attention: no matter is `(self.view.bounds.size.width / 2) - ?(?=1,2,3,4)` it all get the below issue:
但我得到的结果是:
没有流到2列。它流向一个流。
但是如果我在sizeForItemAtIndexPath
方法中设置单元格的大小宽度(self.view.bounds.size.width / 2) - 5
,我会得到下面的效果:
它显示了两个cols,这是我的要求,但是还有一个问题是,右边有一个10px
的space。
如何在 UICollectionView 中避免这个问题?
添加此填充方法。
- (CGSize)collectionView:(UICollectionView *)collectionView layout:(UICollectionViewLayout*)collectionViewLayout sizeForItemAtIndexPath:(NSIndexPath *)indexPath{
return CGSizeMake(SCREEN_WIDTH/2 -5, 89);
}
- (UIEdgeInsets)collectionView:(UICollectionView *)collectionView layout:(UICollectionViewLayout *)collectionViewLayout insetForSectionAtIndex:(NSInteger)section{
return UIEdgeInsetsMake(0, 0, 0, 0);
}
- (CGFloat)collectionView:(UICollectionView *)collectionView layout:(UICollectionViewLayout *)collectionViewLayout minimumInteritemSpacingForSectionAtIndex:(NSInteger)section{
return 10.0f;
}
- (CGFloat)collectionView:(UICollectionView *)collectionView layout:(UICollectionViewLayout *)collectionViewLayout minimumLineSpacingForSectionAtIndex:(NSInteger)section
{
return 10.0f;
}
我设置了我的 UICollectionView 的委托方法:
#pragma mark - collectionView delegate
- (NSInteger)numberOfSectionsInCollectionView:(UICollectionView *)collectionView {
return 1;
}
- (NSInteger)collectionView:(UICollectionView *)collectionView numberOfItemsInSection:(NSInteger)section {
//CGPoint point = self.collectionView.frame.origin;
//CGRect rect = CGRectMake(point.x, point.y, self.collectionView.bounds.size.width, 70*2);
// height
/*_heightOfCollectionView.constant = kHeight_Collection_Cell * 6;
_heightOfContents.constant = collectionView.frame.origin.y + 230 + _heightOfCollectionView.constant;*/
return 12;//_recommendDataArr.count;
}
-(CGSize)collectionView:(UICollectionView *)collectionView layout:(UICollectionViewFlowLayout *)collectionViewLayout sizeForItemAtIndexPath:(NSIndexPath *)indexPath{
float width = (self.view.bounds.size.width / 2);
float height = 89; // 89
return CGSizeMake(width, height);
}
- (CGFloat)collectionView:(UICollectionView *)collectionView layout:(UICollectionViewLayout*)collectionViewLayout minimumLineSpacingForSectionAtIndex:(NSInteger)section
{
return 0;
}
- (UICollectionViewCell *)collectionView:(UICollectionView *)collectionView cellForItemAtIndexPath:(NSIndexPath *)indexPath {
HomeGMCell *cell = [collectionView dequeueReusableCellWithReuseIdentifier:@"HomeGMCell" forIndexPath:indexPath];
if (cell == nil) {
cell = [collectionView dequeueReusableCellWithReuseIdentifier:@"HomeGMCell" forIndexPath:indexPath];
}
return cell;
}
minimumLineSpacingForSectionAtIndex
是 0
:
你看我 return 单元格的宽度是:
(self.view.bounds.size.width / 2) // attention: no matter is `(self.view.bounds.size.width / 2) - ?(?=1,2,3,4)` it all get the below issue:
但我得到的结果是:
没有流到2列。它流向一个流。
但是如果我在sizeForItemAtIndexPath
方法中设置单元格的大小宽度(self.view.bounds.size.width / 2) - 5
,我会得到下面的效果:
它显示了两个cols,这是我的要求,但是还有一个问题是,右边有一个10px
的space。
如何在 UICollectionView 中避免这个问题?
添加此填充方法。
- (CGSize)collectionView:(UICollectionView *)collectionView layout:(UICollectionViewLayout*)collectionViewLayout sizeForItemAtIndexPath:(NSIndexPath *)indexPath{
return CGSizeMake(SCREEN_WIDTH/2 -5, 89);
}
- (UIEdgeInsets)collectionView:(UICollectionView *)collectionView layout:(UICollectionViewLayout *)collectionViewLayout insetForSectionAtIndex:(NSInteger)section{
return UIEdgeInsetsMake(0, 0, 0, 0);
}
- (CGFloat)collectionView:(UICollectionView *)collectionView layout:(UICollectionViewLayout *)collectionViewLayout minimumInteritemSpacingForSectionAtIndex:(NSInteger)section{
return 10.0f;
}
- (CGFloat)collectionView:(UICollectionView *)collectionView layout:(UICollectionViewLayout *)collectionViewLayout minimumLineSpacingForSectionAtIndex:(NSInteger)section
{
return 10.0f;
}