简体   繁体   English

设置集合视图单元格的动态宽度和高度

[英]set Dynamic width and height of collection view cell

Actually i want to set button dynamically and title of button appear Dynamic i have try this and got width of Dynamic content but am not setting cell width and height. 实际上,我想动态设置按钮,并且按钮的标题显示为动态,我已经尝试过此操作并获得了动态内容的宽度,但未设置单元格的宽度和高度。

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

and output is like this 输出是这样的

当我使用此代码时,输​​出看起来像这样

This is perfect i think if i make cell dynamic width and height, so please guide me through this. 我认为如果我使单元动态宽度和高度非常完美,所以请指导我。

pragma mark - GetHeightFromString 实用标记-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;
}

Get height of the string from this method and add this to origional height of your cell or label or button whatever you want to use. 从此方法获取字符串的高度,并将其添加到单元格或标签或按钮想要使用的原始高度。

For example: 例如:

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

You might need to change the height of inner label too, for this use same method in cellForItemAtIndexPath method of your UICollectionView . 您可能需要更改内标签的高度也为在此使用相同的方法cellForItemAtIndexPath你的方法UICollectionView Further more you can customize your own way. 此外,您可以自定义自己的方式。

In Swift 2.0: 在Swift 2.0中:
Using a UICollectionViewFlowLayout enable cell auto-sizing. 使用UICollectionViewFlowLayout启用单元格自动调整大小。
The following code inside a UICollectionViewController subclass will enable auto-sizing for it's flow layout. 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)
    }

} }

You can put different sizes in landscape & portrait mode try this - 您可以在横向和纵向模式下放置不同的尺寸,请尝试以下操作-

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

you can call invalidateLayout method on the .collectionViewLayout property of your UICollectionView 你可以调用invalidateLayout方法对.collectionViewLayout你的财产UICollectionView

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

You can try this... 你可以试试这个...

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

width, height are the dynamically calculated value 宽度,高度是动态计算的值

Thanks 谢谢

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM