objective-c - IOS UICollectionview动态改变单元格的高度

标签 objective-c ios7 uicollectionview

我正在开发基于 Collection View 的项目,该项目的 Numberofitem 将根据用户对特定索引的点击而更改和增加。 all 在我的项目中完美工作,但我无法根据 Collection View 中的单元格数量设置单元格大小。此外,collectionview 将无法滚动所有单元格修复 View 并调整大小。 在这里,我附上了所有图片。

1) Storyboard View

StoryboardView

2) 四格的结果

Result With Four Cell's

3)细胞数增加时

when Number of Cell Increase

目前我用下面的代码通过这种方式完成了我的手机设置。但我想设置一下,如果 Collection View 中有四个单元格,那么每个单元格的大小都会根据 Collection View 和设备大小(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;
}

注意:- 我也试过这所有的方法。

  - (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];
 }

最佳答案

据我了解,您想复制 this 的布局测试。

你需要更好地理解 UICollectionView 是如何工作的 here

您必须在此委托(delegate)方法中更改 UICollectionView 中的项目数:

- (NSInteger)numberOfItemsInSection:(NSInteger)section;

在这里,你想在测试开始时返回 4,然后是 4*2、4*2*2,依此类推。这将为您提供正确数量的项目。

然后是布局。您应该为它设置多个值,因为您不希望元素之间的空间与开始和结束时的空间相同。随着项目数量的增加,我会减少空间。

你需要小心间距,因为它会导致一个单元格被强制放在下一行

通过划分框架以获得单元格的最大高度和宽度,您已经做得很好,但是您的

- (CGSize)collectionView:(UICollectionView *)collectionView
              layout:(UICollectionViewLayout *)collectionViewLayout
  sizeForItemAtIndexPath:(NSIndexPath *)indexPath

方法真乱。

由于您的 UICollectionView 中不会有无限数量的项目,您可以使用 switch 语句,并在每个开关中返回计算的 CGSize 单元格。

例如,你可以这样使用它:

    switch (numberOfHorizontalItems) {
      case 2:
            return CGSizeMake(self.collectionView.frame.size.width/2.5,self.collectionView.frame.size.width/2.5);
                break;

    case 4:
                return CGSizeMake(self.collectionView.frame.size.width/4.5,self.collectionView.frame.size.width/4.5);
                break;
    case 8:
                CGSizeMake(self.collectionView.frame.size.width/8.5,self.collectionView.frame.size.width/8.5);
                break;
(And so on)

      default:
                break;
        }

您应该注意,此处显示的计算 CGSize 必须包含 items 之间的间距/合并

您也不需要 viewWillLayoutSubviewsviewDidLayoutSubviews,您只需要在用户点击时调用 [self.collectionView reloadData]单元格(或在用户点击单元格后处理逻辑的方法中)

最后,如果您不希望UICollectionView 可滚动,只需设置属性self.collectionView.scrollEnabled = NO;

关于objective-c - IOS UICollectionview动态改变单元格的高度,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/32472956/

相关文章:

ios - 创建一个单元格高度大于 View 高度的水平 UICollectionView

ios - UICollectionView 中的固定项目距离

ios - 使用 UICollectionView 放大选择的单元格

iphone - 如何获取 subview 中导航栏的高度?

objective-c - 简单 ios 文字游戏菜单的新 View

iphone - 如何检查 UIView 是否是父 View 的 subview

ios - UICollectionView 在 unhighlightAllItems 上崩溃

ios - 使用 NSIndianCalendar 时显示错误的日期

ios - iOS UIWebView 上的客户端证书身份验证适用于 iOS 6.1 但不适用于 iOS 7

ios7 - NSURLSession 或 NSURLConnection - iOS 6 支持