IOS UICollectionview动态更改Cell的高度 [英] IOS UICollectionview Dynamically change Cell's Height
问题描述
我正在开发基于集合视图的项目,根据用户点击特定索引,将更改Numberofitem并增加其数量。所有在我的项目中完美运行,但我无法根据集合视图中的单元格数设置单元格大小。更多集合视图将无法滚动所有单元格修复视图中的调整大小。
这里我附上了所有图片。
I am working on collection-view Based project on which Numberofitem will be changed and increase according to user's click on Particular Index. all Works perfectly in my project but i am not able to set Cell size according to number of cell's in collection-view.Moreover collectionview will not be scrollable all cell fix in view with resize. Here i have attached all the image's .
1)故事板视图
2)四个单元格的结果
3)当细胞数量增加时
目前我通过这种方式设置了我的细胞以下代码。但我想设置一下,如果在集合视图中有四个单元格,那么每个单元格的大小会根据集合视图和设备大小增加和拟合(iphone 5s,iphone 6,iphone 6plus)
Currently i done setup of my cell by this way with following code. but i want to set that, if there is four cell in collection-view then the size of each cell increases and fit according to collection-view and device size (iphone 5s , iphone 6 , iphone 6plus)
这是我的尝试,
-(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.0;
}
- (CGSize)collectionView:(UICollectionView *)collectionView
layout:(UICollectionViewLayout *)collectionViewLayout
sizeForItemAtIndexPath:(NSIndexPath *)indexPath
{
////@property NSInteger count;////
if (_count == 0) {
// width = ((width - (span*5))/4);
// return CGSizeMake(self.view.frame.size.width/4, self.view.frame.size.height/4);
return CGSizeMake(self.view.frame.size.width/2.5, self.view.frame.size.height/5);
}
if (_count == 1 || _count == 2)
{
return CGSizeMake(self.view.frame.size.width/2.5, self.view.frame.size.height/5);
// return CGSizeMake(100.0, 100.0);
}
if ( _count == 3 || _count == 4 || _count == 5)
{
// return CGSizeMake(100.0, 100.0);
return CGSizeMake(self.view.frame.size.width/3.5, self.view.frame.size.height/6.5);
}
if ( _count == 6 || _count == 7 || _count == 8 || _count == 9)
{
//return CGSizeMake(90.0, 90.0);
return CGSizeMake(self.view.frame.size.width/4, self.view.frame.size.height/8);
}
if ( _count == 10 || _count == 11 || _count == 12 || _count == 13 || _count == 14)
{
// return CGSizeMake(80.0, 80.0);
return CGSizeMake(self.view.frame.size.width/4, self.view.frame.size.height/8);
}
if ( _count == 15 || _count == 16 || _count == 17 || _count == 18 || _count == 19 || _count ==20)
{
//return CGSizeMake(70.0, 70.0);
return CGSizeMake(self.view.frame.size.width/4.75, self.view.frame.size.height/9.5);
}
if (_count ==21 || _count == 22 || _count == 23 || _count == 24 || _count == 25 || _count == 26 || _count == 27) {
// return CGSizeMake(60.0, 60.0);
return CGSizeMake(self.view.frame.size.width/6, self.view.frame.size.height/12);
}
if (_count ==28 || _count == 29 ) {
// return CGSizeMake(50.0, 50.0);
return CGSizeMake(self.view.frame.size.width/6.25, self.view.frame.size.height/12.5);
}
else
return CGSizeMake(0, 0);
}
- (NSInteger)collectionView:(UICollectionView *)collectionView numberOfItemsInSection:(NSInteger)section
{
return itemincollectionview.count;
}
注意: -
我有也尝试了所有方法。
Note:- I have also tried this all Method.
- (void)viewDidLayoutSubviews
{
self.automaticallyAdjustsScrollViewInsets = NO;
[self.collectionView layoutIfNeeded];
NSArray *visibleItems = [self.collectionView indexPathsForVisibleItems];
NSIndexPath *currentItem = [visibleItems objectAtIndex:0];
NSIndexPath *nextItem = [NSIndexPath indexPathForItem:_setRandomIndex inSection:currentItem.section];
[self.collectionView scrollToItemAtIndexPath:nextItem atScrollPosition:UICollectionViewScrollPositionNone animated:YES];
}
- (void)viewWillLayoutSubviews
{
[super viewWillLayoutSubviews];
[self.collectionView.collectionViewLayout invalidateLayout];
}
-(void)viewWillAppear:(BOOL)animated
{
[super viewWillAppear:YES];
[self.view layoutIfNeeded];
}
推荐答案
项目中的一切都很好只需要添加一个新的委托方法,如下所示:
Everything in your project is fine just you need to add a new delegate method as follows:
- (UIEdgeInsets)collectionView:(UICollectionView *)collectionView layout:(UICollectionViewLayout*)collectionViewLayout insetForSectionAtIndex:(NSInteger)section {
return UIEdgeInsetsMake(15, 20, 20, 20);
}
根据您拥有的计数数量,此方法会有所不同。插入设置将是常见的:
Vary this method insets according to number of counts of you have. inset setting will be common for:
self.view.frame.size.width/2.5
并且不同于:
self.view.frame.size.width/3.5
因此额外的间距问题将得到解决
hence the extra spacing issue will be solved
这篇关于IOS UICollectionview动态更改Cell的高度的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!