3

각 셀 상단에 셀이있는 간단한 UICollectionViewCell 스타일이 필요합니다. tableview와 같습니다. 하지만 콘텐츠의 동적 높이 의존성, 콘텐츠의 크기가 달라질 수 있습니다. 내가 가진콘텐츠에서 동적 uicollectionviewcell 크기를 설정하는 방법 - 프로그래밍 방식으로

[self.commentsCollectionView registerClass:[GWCommentsCollectionViewCell class] forCellWithReuseIdentifier:@"commentCell"]; 

.H에서 :

나는 # import를 내 사용자 지정 UICollectionViewCell 프로그램 자동 레이아웃 모든 제약 조건을 설정

나는 의 viewDidLoad을 얻었다.

UICollectionViewFlowLayout *collViewLayout = [[UICollectionViewFlowLayout alloc]init]; 
self.commentsCollectionView = [[UICollectionView alloc]initWithFrame:CGRectZero collectionViewLayout:collViewLayout]; 

은 내가 (그게 왜 CGRectZero를) 할 위치를 UICollectionView 될 얻을 autolatyout를 사용

내가 가진 UICollectionView를 인스턴스화합니다.

그리고 마지막으로 내가이 일을 기대했다 :

-(CGSize)collectionView:(UICollectionView *)collectionView layout:(UICollectionViewLayout *)collectionViewLayout sizeForItemAtIndexPath:(NSIndexPath *)indexPath{ 

    GWCommentsCollectionViewCell *cell = (GWCommentsCollectionViewCell*)[collectionView cellForItemAtIndexPath:indexPath]; 

    return cell.singleCommentContainerview.bounds.size; 
} 

singleCommentContainerview 모든 autolayoutcode 패널 컴퓨터 설정 직접있는 contentView의 서브 뷰와 withing에 내가 UIImageViews 등 UILabels을 가지고있는 singleCommentContainerview입니다.

하지만 단지 (0,0)

의 cgsize의 가치를 어떻게 이것이 내가 각 셀에 필요한 적절한 크기를 얻을 해결할 수 있습니까?

답변

0

내가 읽은 것에서 UICollectionView는 셀을 레이아웃하기 전에 계산 된 크기가 필요합니다. 그래서 위의 방법은 세포가 아직 그려지지 않았으므로 크기가 없습니다. 또한 그것은 문제이거나 셀이 동일한 식별자 @ "commentCell"로 캐싱/풀링되는 문제와 결합 될 수 있습니다. 새 식별자와 클래스로 고유 셀에 태그를 붙입니다.

- (void)collectionView:(UICollectionView *)collectionView 
     willDisplayCell:(UICollectionViewCell *)cell 
    forItemAtIndexPath:(NSIndexPath *)indexPath{ 

GWCommentsCollectionViewCell *cell = (GWCommentsCollectionViewCell*)[collectionView cellForItemAtIndexPath:indexPath]; 
// Need to add it to the view maybe in order for it the autolayout to happen 
[offScreenView addSubView:cell]; 
[cell setNeedsLayout]; 

CGSize *cellSize=cell.singleCommentContainerview.bounds.size 
NSString *key=[NSString stringWithFormat:@"%li,%li",indexPath.section,indexPath.row]; 
// cellAtIndexPath is a NSMutableDictionary initialised and allocated elsewhere 
[cellAtIndexPath setObject:[NSValue valueWithCGSize:cellSize] forKey:key]; 

} 

을 그럼 당신은 그것이를 얻기 위해 기반으로 키를 그 사전을 사용할 필요가있을 때 :

내 생각은 사용이 그려지기 전에, 셀을 잡을 나중에 사용하기 위해 사전에 크기를 밀어 수 있습니다 크기.

뷰가 그려지는 의존성이 정말 멋지다. 크기를 얻으려면 autolayout이 먼저해야한다. 이미지를 더 많이로드하는 경우 문제가 발생할 수 있습니다.

더 좋은 방법은 크기를 미리 프로그래밍하는 것입니다. 이미지 크기에 도움이 될만한 데이터가있는 경우.(야아, 프로그램에는 IB) 정말 좋은 튜토리얼은이 문서를 확인하지 :

https://bradbambara.wordpress.com/2014/05/24/getting-started-with-custom-uicollectionview-layouts/

+0

나는 완벽한 솔루션을 곧 제공 할 예정입니다. – elliotrock

-1

사용자 정의 셀에

class func size(data: WhateverYourData) -> CGSize { /* calculate size here and  retrun it */} 

을 추가하고 대신이어야

return cell.singleCommentContainerview.bounds.size 

을하는

return GWCommentsCollectionViewCell.size(data) 
+0

언어가 잘못되었습니다. 문제는 objective-c에 관한 것입니다. – picciano

+0

@picciano - oops. 하하. – HMHero

관련 문제