2013-07-31 10 views
0

그래서 내가 아는 한, "프로토콜 방법"UICollectionViewCell의 위치는 어떻게 설정합니까?

(UICollectionViewCell *)collectionView:(UICollectionView *)collectionView cellForItemAtIndexPath:(NSIndexPath *)indexPath 

자동으로 내가 만든 셀의 경계를 설정합니다. 이것은 모두 UICollectionViewCells에 대한 모든 훌륭한 및 멋쟁이지만, 나는 그들 중 하나가 내가 지정한 위치에 있어야합니다. 올바른 방향으로 향한 어떤 포인터라도 감사 할 것입니다.

답변

0

또한 컬렉션보기의 레이아웃 객체로 UICollectionViewFlowLayout의 인스턴스를 사용한다고 가정 할 수 있습니까?

- (UICollectionViewLayoutAttributes *)layoutAttributesForItemAtIndexPath:(NSIndexPath *)indexPath 
{ 
    if (indexPath.section == 0 && indexPath.item == 2) // or whatever specific item you're trying to override 
    { 
     UICollectionViewLayoutAttributes *layoutAttributes = [UICollectionViewLayoutAttributes layoutAttributesForCellWithIndexPath:indexPath]; 
     layoutAttributes.frame = CGRectMake(0,0,100,100); // or whatever... 
     return layoutAttributes; 
    } 
    else 
    { 
     return [super layoutAttributesForItemAtIndexPath:indexPath]; 
    } 
} 

당신은 아마 또한 -layoutAttributesForElementsInRect:를 오버라이드 (override) 할 필요가 있습니다 :

- (NSArray *)layoutAttributesForElementsInRect:(CGRect)rect 
{ 
    NSArray *layoutAttributes = [super layoutAttributesForElementInRect:rect]; 
    if (CGRectContainsRect(rect, CGRectMake(0, 0, 100, 100))) // replace this CGRectMake with the custom frame of your cell... 
    { 
     UICollectionViewLayoutAttributes *layoutAttributes = [UICollectionViewLayoutAttributes layoutAttributesForCellWithIndexPath:indexPath]; 
     layoutAttributes.frame = CGRectMake(0,0,100,100); // or whatever... 
     return [layoutAttributes arrayByAddingObject:layoutAttributes]; 
    } 
    else 
    { 
     return layoutAttributes; 
    } 
} 

그런 다음 새로운 서브 클래스를 사용

는 그렇다면, 빠르고 더러운 대답은 UICollectionViewFlowLayout를 서브 클래스와 -layoutAttributesForItemAtIndexPath: 메소드를 오버라이드 (override)하는 것입니다 컬렉션보기를 만들 때 UICollectionViewFlowLayout 대신.

+0

알겠습니다. 감사합니다. 나는 이미 [lxreorderablecollectionviewflowlayout] (https://github.com/lxcid/LXReorderableCollectionViewFlowLayout)이라 불리는 UICollectionViewFlowLayout의 서브 클래스를 사용하고 있었지만 제공 한 것들과 함께 기존 코드를 가지고 놀았습니다. layoutAttributesForElementsInRect 메서드의 – user2539621

+1

NSArray * layoutAttributes = [super layoutAttributesForElementInRect : rect]; NSArray로 대체되어야합니다 * layoutAttributes = [super layoutAttributesForElementsInRect rect]; – jerrygdm

+0

@JoshHinman layoutAttributesForElementsInRect가 수행하는 작업과 그 이유를 재정의해야 할 이유를 설명 할 수 있습니까 – Stefan

관련 문제