셀 (특히 UILabel)의 내용 크기에 따라 UICollectionViewCell의 높이를 변경하고 싶습니다. 난 후 두 번째 방법은 첫 번째 전에 호출 나타나는 UICollectionViewCell의 높이 설정
- (CGSize)collectionView:(UICollectionView *)collectionView
layout:(UICollectionViewLayout *)collectionViewLayout
sizeForItemAtIndexPath:(NSIndexPath *)indexPath
에 UICollectionViewCell 높이를 설정
-(UICollectionViewCell *)collectionView:(UICollectionView *)collectionViewcellForItemAtIndexPath:(NSIndexPath *)indexPath
에 UILabel의 높이를 설정하므로 라벨의 높이가 아직 계산되지 않은 . 전체 코드는 아래에서 찾을 수 있습니다 (경고 : 깔끔함이 필요함). 어떤 아이디어?
-(UICollectionViewCell *)collectionView:(UICollectionView *)collectionView
cellForItemAtIndexPath:(NSIndexPath *)indexPath
{
UICollectionViewCell *myCell = [collectionView
dequeueReusableCellWithReuseIdentifier:@"Cell"
forIndexPath:indexPath];
label = [[UILabel alloc] initWithFrame:CGRectMake(80,10,width - 90,0)];
label.text = @"Hellfoneofinerigoneriognwirotngwtngowirnthointgonowenfkqejb fgjkreb glknrlegkn ewlj qerlgjnweofjpeorihgpireghiperhgorgrngl;rtnh;ltm;l";
label.lineBreakMode = NSLineBreakByWordWrapping;
label.numberOfLines = 0;
label.font = [UIFont fontWithName:@"HelveticaNeue-UltraLight" size:17];
[label sizeToFit];
[myCell.contentView addSubview:label];
CGSize labelY = [label bounds].size;
i = labelY.height;
NSLog(@"Height: %d", i);
return myCell;
}
#pragma mark <UICollectionViewDelegate>
- (CGSize)collectionView:(UICollectionView *)collectionView
layout:(UICollectionViewLayout *)collectionViewLayout
sizeForItemAtIndexPath:(NSIndexPath *)indexPath
{
NSLog(@"Height: %d", i);
if(UI_USER_INTERFACE_IDIOM() == UIUserInterfaceIdiomPhone)
{
CGSize result = [[UIScreen mainScreen] bounds].size;
if(result.height == 568)
{
return CGSizeMake(300.f, i);
}
if(result.height == 667)
{
return CGSizeMake(340.f, i);
}
if(result.height == 736)
{
return CGSizeMake(370.f, i);
}
}
return CGSizeMake(300.f, i);
}
편집 : 오류를 설명하지 못했습니다. -(UICollectionViewCell *)collectionView:(UICollectionView *)collectionView cellForItemAtIndexPath:(NSIndexPath *)indexPath .
을 호출하면 i의 높이가 0입니다.