[英]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.