2014-10-11 4 views
0

셀 (특히 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입니다.

답변

0

높이 메서드에서 Label을 만들고 거기에서 높이를 결정하여 작업했습니다.

0

- (CGSize)collectionView:(UICollectionView *)collectionView layout:(UICollectionViewLayout *)collectionViewLayout sizeForItemAtIndexPath:(NSIndexPath *)indexPath
는 소스 방법 -(UICollectionViewCell *)collectionView:(UICollectionView *)collectionView cellForItemAtIndexPath:(NSIndexPath *)indexPath, 모든 셀의 크기 sizeForItemAtIndexPath 계산보다 먼저 실행된다있어서 레이아웃 대리인 후이를 구축하기 시작.

그래서, 당신은 단지 다음 표시 높이를 계산 indexPath하여 컨텐츠를 취득 당신이 구현하려는 어떤 cellForItemAtIndexPath

0

에 높이 값을 적용해야 UICollectionViewCell의 자동 크기 조정입니다.

  • 선행 및 후행 가장자리
  • 마지막 레이블의 Vertical Content Hugging Priority 250
  • 있는지 확인, 상단, 하단에 FlowLayout의
  • 핀에 대한 레이블을 estimatedItemSize를 설정 : 당신이 필요로 구현하려면 당신의 UICollectionViewCell

에서

  • 및 구현 preferredLayoutAttributesFitting 나는 UICollectinViewCell의 여기에 자체 크기 조정에 대한 이상 블로그 게시물을 작성했습니다 : http://parveenkaler.com/posts/self-sizing-uicollectionviewcell-swift