简体   繁体   English

如何在iOS中动态更改UICollectionView单元格高度

[英]How can change UICollectionView cell height dynamically in iOS

I am creating on UICollectionView for showing some data which are coming from server. 我在UICollectionView创建,用于显示来自服务器的一些数据。 But I have to set that cell height dynamically according to text size. 但我必须根据文本大小动态设置单元格高度。 I am creating a custom cell for UICollectionView . 我正在为UICollectionView创建一个自定义单元格。

Retrieve that UICollectionView cell in my ViewDidLoad method: 在我的ViewDidLoad方法中检索该UICollectionView单元格:

UINib *cellNib = [UINib nibWithNibName:@"Custom_CollectionViewCell" bundle:nil];
[self.noteBookmarkCollectinView registerNib:cellNib forCellWithReuseIdentifier:@"CustomCell"];

Below Delegate method for UICollectionView : 下面是UICollectionView Delegate方法:

 #pragma mark Collection view layout things

 -(NSInteger)numberOfSectionsInCollectionView:(UICollectionView *)collectionView {
    return 1;
 }
 -(NSInteger)collectionView:(UICollectionView *)collectionView numberOfItemsInSection:(NSInteger)section
 {
    return noteDetailsArr.count;
 }    
 - (CGFloat)collectionView:(UICollectionView *)collectionView layout:(UICollectionViewLayout*)collectionViewLayout minimumInteritemSpacingForSectionAtIndex:(NSInteger)section {
    return 4.0;
 }
 - (CGFloat)collectionView:(UICollectionView *)collectionView layout:(UICollectionViewLayout*)collectionViewLayout minimumLineSpacingForSectionAtIndex:(NSInteger)section {
    return 4.0;
 }
 - (UIEdgeInsets)collectionView:(UICollectionView *)collectionView layout:(UICollectionViewLayout*)collectionViewLayout insetForSectionAtIndex:(NSInteger)section
 {
    return UIEdgeInsetsMake(0,4,0,4);  // top, left, bottom, right
 }
 - (CGSize)collectionView:(UICollectionView *)collectionView layout:(UICollectionViewLayout*)collectionViewLayout sizeForItemAtIndexPath:(NSIndexPath *)indexPath
 {
    return CGSizeMake(154, 154); // This is my fixed Cell height

   //Before I am trying this below code also, but its not working
    return [(NSString*)[contentArr objectAtIndex:indexPath.row] sizeWithAttributes:NULL];
 }
 -(UICollectionViewCell *)collectionView:(UICollectionView *)collectionView cellForItemAtIndexPath:(NSIndexPath *)indexPath
 {
    static NSString *cellIdentifier = @"CustomCell";

    Custom_CollectionViewCell *cell = [collectionView dequeueReusableCellWithReuseIdentifier:cellIdentifier forIndexPath:indexPath];
   cell.contentLbl.numberOfLines = 0;
   cell.contentLbl.tag = indexPath.row;

   cell.contentLbl.text = [contentArr objectAtIndex:indexPath.row];
   [cell.contentLbl sizeToFit];

   return cell;
}

My result is coming like this : 我的结果是这样的:

在此输入图像描述

Notes: Green color is Cell background color & Pink color is label BG Color. 注意:绿色是单元格背景颜色,粉红色是标签BG颜色。

And when I scroll the CollectionView I am able to see like below Image & After scroll again its coming perfect (Only for last row) 当我滚动CollectionView我能够看到如下Image&After再次滚动它即将完美(仅限最后一行)

在此输入图像描述

How can I make the cell height dynamically according to text. 如何根据文本动态制作单元格高度。 please Help me. 请帮我。 I am stuck from last 1 week. 我被困在最后一周。

hope it helps 希望能帮助到你

#pragma mark <UICollectionViewDelegate>
- (CGSize)collectionView:(UICollectionView *)collectionView
                  layout:(UICollectionViewLayout*)collectionViewLayout
  sizeForItemAtIndexPath:(NSIndexPath *)indexPath{

    NSString * string = [self.array objectAtIndex:indexPath.row]


    //CALCULATE text SIZE:
    CGSize textSize = [string sizeWithAttributes:NULL];
    return textSize;
}

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

相关问题 如何动态更改UICollectionview单元格高度? - How to change UICollectionview Cell Height dynamically? 如何在Swift中动态更改包含UICollectionView单元格的UITableView单元格的高度? - How to dynamically change the height of a UITableView Cell containing a UICollectionView cell in Swift? 根据单元格不改变单元格高度动态地更改UICollectionView高度 - Change UICollectionView Height Dynamically according to cell's not change cell height 动态更新uicollectionview单元格高度 - Dynamically update uicollectionview cell height 动态更改单元格间距UICollectionView - Dynamically change cell spacing UICollectionView iOS:UICollectionView 动画高度变化 - iOS: UICollectionView animate height change 如何根据Swift3中的内容自动更改UICollectionView Cell的高度? - How to change the height of UICollectionView Cell automatically according to content in Swift3? iOS - 如何在 UICollectionView 中动态插入之前正确重新加载单元格 - iOS - How to properly reload a cell before inserting it dynamically in UICollectionView 如何动态改变UICollectionView的高度以适应内容大小 - How to dynamically change the height of UICollectionView to adapt to content size 计算iOS中水平UICollectionView的动态单元格高度 - Calculate dynamic cell height for a horizontal UICollectionView in iOS
 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM