4

コレクション ビュー セルをフェッチしている間、なぜ私のコードがうまく機能するのか、うまく機能しcellForItemAtIndexPath:ないのか疑問に思っていました。dequeueReusableCellWithReuseIdentifier:

これが私のコードです:

これはうまくいきます:

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..
        }

何か案は?

4

2 に答える 2

6

これら 2 つは、基本的に 2 つの非常に異なる方法です。

  1. dequeReusableCellWithReuseIdentifier: 表示する記事のリストがあるとします。記事が 50 件あるとします。画面には、一度に 50 件の記事がすべて表示されるわけではありません。行に指定した高さに基づいて、限られたセルが一度に表示されます。画面に一度に 5 つの記事しか表示されず、リストの一番上にいるとします。リストには項目 1 ~ 5 が表示されます。スクロールすると、6番目のアイテムを表示するために、リストは最初のセルを再利用し、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;

}

まだ不明な点がありましたらお知らせください。

于 2014-05-01T05:21:47.763 に答える
3

それらは異なるものです:

  • cellForItemAtIndexPathコレクション ビューから既に入力されているセルを取得します。
  • dequeueReusableCellWithReuseIdentifier新しいセルに入力するときに、再利用できるセルを返す可能性があります。存在する細胞オブジェクトの数を減らすのに役立つように設計されています。失敗した場合 (多くの場合失敗します)、新しいセルを明示的に作成する必要があります。
于 2014-05-01T05:08:22.613 に答える