4

내 코드가 cellForItemAtIndexPath: &이 아닌 dequeueReusableCellWithReuseIdentifier:으로 잘 작동하는지 궁금해하고 있습니다. 컬렉션 뷰 셀을 가져 오는 중입니다. 이 잘 컴파일하지만 (I 셀에서 수행하는 변경 사항이 묘사되지 않음)dequeueReusableCellWithReuseIdentifier와 cellForItemAtIndexPath의 차이점 :

를 작동하지 않는 동안

NSInteger numberOfCells = [self.collectionView numberOfItemsInSection:0]; 
    for (NSInteger i = 0; i < numberOfCells; i++) { 
     myCustomCollectionCell *cell = (myCustomCollectionCell *)[self.collectionView cellForItemAtIndexPath:[NSIndexPath indexPathForItem:i inSection:0]]; 
     //here I use the cell.. 
    } 

:

이 하나가 잘 작동 : 여기

내 코드입니다
NSInteger numberOfCells = [self.collectionView numberOfItemsInSection:0]; 
     for (NSInteger i = 0; i < numberOfCells; i++) { 
      myCustomCollectionCell *cell = [self.collectionView dequeueReusableCellWithReuseIdentifier:@"myCell"forIndexPath:[NSIndexPath indexPathForItem:i inSection:0]]; 
      //here I use the cell.. 
     } 

시도해 보았습니다. 그러나 아무 사용 :

NSInteger numberOfCells = [self.collectionView numberOfItemsInSection:0]; 
     for (NSInteger i = 0; i < numberOfCells; i++) { 
      myCustomCollectionCell *cell = (myCustomCollectionCell *)[self.collectionView dequeueReusableCellWithReuseIdentifier:@"myCell"forIndexPath:[NSIndexPath indexPathForItem:i inSection:0]]; 
      //here I use the cell.. 
     } 

어떤 아이디어?

+0

dequeueReusableCellWithReuseIdentifier를 사용하기위한 전제 조건은 registerClass : forCellReuseIdentifier를 사용하는 것입니다. - 수행 했습니까? 동일한 재사용 식별자를 사용하고 있습니까? –

+0

셀 클래스를 등록하지 않고 'dequeueReusableCellWithReuseIdentifier'의 indexPath가 아닌 버전을 사용할 수 있습니다. 무제한으로 구현하려면 기본 iOS 샘플 코드를 살펴 봐야합니다. – Neil

+0

@neil .. 너를 얻지 못 했어 .. –

답변

6

이 두 가지는 기본적으로 두 가지 매우 다른 방법입니다.

  1. dequeReusableCellWithReuseIdentifier : 보려는 기사 목록이 있다고 가정합니다. 50 개의 기사가 있다고 가정 해보십시오. 화면에는 한 번에 화면에 모든 50 개의 기사가 표시되지 않습니다. 행에 지정한 높이를 기준으로 한 번에 제한된 셀을 표시합니다. 한 번에 5 개의 기사 만 화면에 표시하고 이제는 목록의 상단에 있습니다. 목록에 1-5 항목이 표시됩니다. 이제 6 번째 항목을 표시하려면 inorder를 스크롤하면 첫 번째 셀이 목록에서 재사용되고 6 번째 항목에 맞게 구성되어 표시됩니다. 이 시간까지는 첫 번째 셀이 보이지 않습니다.

2. cellForRowAtIndexPath : 반면 cellForRowAtIndexPath 반환 당신에게보기 또는 귀하가 제공 IndexPath에서 이미 셀에. 이 경우에 셀이 이미 메모리에 있다면, 그 셀을 반환하거나 새로운 셀을 구성하여 리턴합니다.

아래 예제는 UITableViews에 대한 것이지만 UICollectionViews는 같은 방식으로 처리 할 수 ​​있습니다.

- (UITableViewCell *)tableView:(UITableView *)tableView cellForRowAtIndexPath:(NSIndexPath *)indexPath { 

    /* 
    * This is an important bit, it asks the table view if it has any available cells 
    * already created which it is not using (if they are offscreen), so that it can 
    * reuse them (saving the time of alloc/init/load from xib a new cell). 
    * The identifier is there to differentiate between different types of cells 
    * (you can display different types of cells in the same table view) 
    */ 

    UITableViewCell *cell = [tableView dequeueReusableCellWithReuseIdentifier:@"MyIdentifier"]; 

    /* 
    * If the cell is nil it means no cell was available for reuse and that we should 
    * create a new one. 
    */ 
    if (cell == nil) { 

     /* 
     * Actually create a new cell (with an identifier so that it can be dequeued). 
     */ 

     cell = [[[UITableViewCell alloc] initWithStyle:UITableViewCellStyleSubtitle reuseIdentifier:@"MyIdentifier"] autorelease]; 

    } 

    /* 
    * Now that we have a cell we can configure it to display the data corresponding to 
    * this row/section 
    */ 

    //Configure the cell here.. 


    /* Now that the cell is configured we return it to the table view so that it can display it */ 


    return cell; 

} 

내가 아직 불분명 한 경우 알려주십시오.

+0

대답은 고맙습니다.하지만 이것은 내가 묻는 것이 아닙니다. 코드를보고 & 그 이유가 무엇인지 말해주세요. –

+0

:) 대답은 위의 답변 자체에 있습니다. 'dequeueReusableCellWithReuseIdentifier : forIndexPath :'는 'cellForRowAtIndexPath :'가 항상 셀을 반환하고 날씨가 메모리에 있는지 여부와는 달리 셀이 메모리에 이미있는 경우에만 셀을 대기열에서 제외합니다. https://developer.apple.com/library/ios/documentation/uikit/reference/UITableView_Class/Reference/Reference.html#//apple_ref/occ/instm/UITableView/dequeueReusableCellWithIdentifier:forIndexPath :이 링크의 토론을 확인하십시오 –

+0

예 .. 나는 그것을 이해한다. 나는 단지 알 필요가있다, "dequeReusableCell .."을 어떻게 사용할 수 있는가 .. –

3

그들은 다른 것들 :

  • cellForItemAtIndexPath 컬렉션보기에서 이미 채워진 셀을 가져옵니다.
  • dequeueReusableCellWithReuseIdentifier은 새 셀을 채울 때 다시 사용할 수있는 셀을 반환합니다. 이것은 존재하는 셀 객체의 수를 줄이는 데 도움이되도록 설계되었습니다. 실패 할 경우 (종종 그렇게 될 것입니다) 새 셀을 명시 적으로 만들어야합니다.