如何在iOS中动态更改UICollectionView单元格高度
我正在如何在iOS中动态更改UICollectionView单元格高度,ios,objective-c,uicollectionviewcell,Ios,Objective C,Uicollectionviewcell,我正在UICollectionView上创建,以显示来自服务器的一些数据。但我必须根据文本大小动态设置单元格高度。我正在为UICollectionView创建自定义单元格 在myViewDidLoad方法中检索该UICollectionView单元格: UINib *cellNib = [UINib nibWithNibName:@"Custom_CollectionViewCell" bundle:nil]; [self.noteBookmarkCollectinView registerNi
UICollectionView
上创建,以显示来自服务器的一些数据。但我必须根据文本大小动态设置单元格高度。我正在为UICollectionView
创建自定义单元格
在myViewDidLoad
方法中检索该UICollectionView
单元格:
UINib *cellNib = [UINib nibWithNibName:@"Custom_CollectionViewCell" bundle:nil];
[self.noteBookmarkCollectinView registerNib:cellNib forCellWithReuseIdentifier:@"CustomCell"];
下面是UICollectionView
的委托方法:
#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;
}
我的结果如下:
注意:绿色是单元格背景色,粉色是标签背景色。
当我滚动CollectionView
时,我可以看到下面的图像&在再次滚动之后,它将变得完美(仅适用于最后一行)
如何根据文本动态设置单元格高度。请帮帮我。我从上个星期就被卡住了。希望能有所帮助
#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;
}
#pragma标记
-(CGSize)collectionView:(UICollectionView*)collectionView
布局:(UICollectionViewLayout*)collectionViewLayout
SizeFormitedIndeXPath:(NSIndexPath*)indexPath{
NSString*string=[self.array objectAtIndex:indexath.row]
//计算文本大小:
CGSize textSize=[字符串大小属性:NULL];
返回文本大小;
}
这是一个处理此问题的好例子。请单击[此处][1]。[1] 当前位置我已经试过这个了,但它不起作用man@CatalinaT:我需要根据文本,单元格高度与标签高度相同