Difference between dequeueReusableCellWithReuseIdentifier: and cellForItemAtIndexPath:

I was wondering why my code works well with cellForItemAtIndexPath:and not with dequeueReusableCellWithReuseIdentifier:when extracting collection cells.

Here is my code:

This works fine:

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

So far this compiles well, but does not work (the changes I make on the cell are not displayed)

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

Tried this too , but did not use:

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

Any ideas?

+4
source share
2 answers

These two are basically two very different ways.

  • dequeReusableCellWithReuseIdentifier: , . , 50 . 50 . , , . , 5 , . 1-5. , , 6- , , 6- . 1- .

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;

}

, .

+6

:

  • cellForItemAtIndexPath .
  • dequeueReusableCellWithReuseIdentifier, , , . . ( ), .
+3

Source: https://habr.com/ru/post/1538817/


All Articles