【问题标题】:set Dynamic width and height of collection view cell设置集合视图单元格的动态宽度和高度
【发布时间】:2016-08-05 09:28:59
【问题描述】:

实际上我想动态设置按钮并且按钮的标题出现动态我已经尝试过这个并获得了动态内容的宽度但我没有设置单元格的宽度和高度。

- (void)viewDidLoad {
    [super viewDidLoad];

    ContentArray = [[NSMutableArray alloc]init];

    self.collectionview.delegate = self;
    self.collectionview.dataSource =self;
    self.collectionview.backgroundColor = [UIColor clearColor];


    [ContentArray addObject:@"Planning"];
    [ContentArray addObject:@"Resources"];
    [ContentArray addObject:@"Human Resources"];
    [ContentArray addObject:@"Agriculture"];
    [ContentArray addObject:@"Resources management"];


}

- (NSInteger)collectionView:(UICollectionView *)collectionView numberOfItemsInSection:(NSInteger)section
    {
        return ContentArray.count;
    }


    - (UICollectionViewCell *)collectionView:(UICollectionView *)collectionView cellForItemAtIndexPath:(NSIndexPath *)indexPath
    {

        CollectionViewCell * Cell = [collectionView dequeueReusableCellWithReuseIdentifier:@"CollectionViewCell"forIndexPath:indexPath];

        [Cell.button setTitle:[ContentArray objectAtIndex:indexPath.row] forState:UIControlStateNormal];

        return Cell;
    }


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


        CGRect rect = [[ContentArray objectAtIndex:indexPath.row ] boundingRectWithSize:CGSizeMake(HUGE_VALF, 50) options:NSStringDrawingUsesFontLeading attributes:nil context:nil] ;


        return CGSizeMake(rect.size.width, 50);
    }

输出是这样的

如果我让单元格动态宽度和高度,我认为这是完美的,所以请指导我完成这个。

【问题讨论】:

  • 你想让单元格宽度足够大以显示单元格文本吗?
  • 或单元格+标签高度显示全文?
  • 在单元格中显示全文
  • 您需要调整单元格和单元格内标签的大小
  • 问题应该很清楚,guide me through this 听起来不太清楚。

标签: ios objective-c uicollectionview uicollectionviewcell uicollectionviewlayout


【解决方案1】:

pragma mark - GetHeightFromString

- (CGFloat)getTextHeightFromString:(NSString *)text ViewWidth:(CGFloat)width WithPading:(CGFloat)pading FontName:(NSString *)fontName AndFontSize:(CGFloat)fontSize
{
    NSDictionary *attributes = @{NSFontAttributeName: [UIFont fontWithName:fontName size:fontSize]};
    CGRect rect = [text boundingRectWithSize:CGSizeMake(width, MAXFLOAT)
                                     options:NSStringDrawingUsesLineFragmentOrigin
                                  attributes:attributes
                                     context:nil];
    //NSLog(@"rect.size.height: %f", rect.size.height);
    return rect.size.height + pading;
}

从此方法获取字符串的高度,并将其添加到您想要使用的单元格或标签或按钮的原始高度。

例如:

  - (CGSize)collectionView:(UICollectionView *)collectionView layout:(UICollectionViewLayout*)collectionViewLayout sizeForItemAtIndexPath:(NSIndexPath *)indexPath
    {
        YourCellClass *myCell =  (YourCellClass*) [collectionView cellForItemAtIndexPath:indexPath];  

        CGFloat  sizeFromString   = [self getTextHeightFromString:itemObject.MenuDescription ViewWidth:myCell.frame.size.width WithPading:10 FontName:@"HelveticaNeue" AndFontSize:13];
    
        return CGSizeMake(sizeFromString, sizeFromString);
    }

您可能还需要更改内部标签的高度,为此在cellForItemAtIndexPath 方法中使用与UICollectionView 相同的方法。 此外,您还可以自定义自己的方式。

【讨论】:

  • -cellForItemAtIndexPath: 方法接受 NSIndexPath 对象。您正在传递indexPath.row - 这是错误的。它不会编译。
  • 但是如何获取 NSString 的宽度,我尝试应用此代码,但它只是在屏幕上显示我的数组的第一个字符串。 return CGSizeMake(sizeFromString, sizeFromString); 返回一个具有两个高度值的正方形。
【解决方案2】:

在 Swift 2.0 中:
使用 UICollectionViewFlowLayout 启用单元格自动调整大小。
UICollectionViewController 子类中的以下代码将为其流布局启用自动调整大小。

override func viewDidLoad() {
    super.viewDidLoad()
    // Do any additional setup after loading the view, typically from a nib. 
    if let cvl = collectionViewLayout as? UICollectionViewFlowLayout {
        cvl.estimatedItemSize = CGSize(width: 150, height: 75)
    }

}

【讨论】:

  • estimatedItemSize 在 Swift 3.0 中添加新行时出现布局问题。请检查。
【解决方案3】:

您可以在横向和纵向模式下放置不同的尺寸试试这个 -

- (CGSize)collectionView:(UICollectionView *)collectionView
                  layout:(UICollectionViewLayout *)collectionViewLayout
  sizeForItemAtIndexPath:(NSIndexPath *)indexPath
{    
    // Adjust cell size for orientation
    if (UIDeviceOrientationIsLandscape([[UIApplication sharedApplication] statusBarOrientation])) {
        return CGSizeMake(rect.size.width, 50);
    }
    return CGSizeMake(rect.size.width, 50);
}

- (void)didRotateFromInterfaceOrientation:(UIInterfaceOrientation)fromInterfaceOrientation
{
    [self.collectionView performBatchUpdates:nil completion:nil];
}

您可以在UICollectionView.collectionViewLayout 属性上调用invalidateLayout 方法

- (void)viewDidLayoutSubviews {
    [super viewDidLayoutSubviews];
    [yourCollectionView.collectionViewLayout invalidateLayout];
}

【讨论】:

  • 嘿,这个不工作,实际上我必须返回 return CGSizeMake(rect.size.width, 50);在 - (CGSize)collectionView:(UICollectionView *)collectionView 布局:(UICollectionViewLayout *)collectionViewLayout sizeForItemAtIndexPath:(NSIndexPath *)indexPath
【解决方案4】:

你可以试试这个……

- (CGSize)collectionView:(UICollectionView *)collectionView layout:(UICollectionViewLayout*)collectionViewLayout sizeForItemAtIndexPath:(NSIndexPath *)indexPath
{
return CGSizeMake(width, height);
}

width,height是动态计算的值

谢谢

【讨论】:

  • 您应该指出如何从 NSString 计算动态宽度和高度。这就是这个问题的实质。
猜你喜欢
  • 1970-01-01
  • 1970-01-01
  • 1970-01-01
  • 1970-01-01
  • 1970-01-01
  • 2020-10-23
  • 2014-04-21
  • 1970-01-01
  • 1970-01-01
相关资源
最近更新 更多