dequeueReusableCellWithReuseIdentifier:和cellForItemAtIndexPath之间的区别: [英] Difference between dequeueReusableCellWithReuseIdentifier: and cellForItemAtIndexPath:

查看:882
本文介绍了dequeueReusableCellWithReuseIdentifier:和cellForItemAtIndexPath之间的区别:的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我一直想知道为什么我的代码适用于 cellForItemAtIndexPath:&获取集合视图单元格时没有 dequeueReusableCellWithReuseIdentifier:

I have been wondering why my code works well with cellForItemAtIndexPath: & not with dequeueReusableCellWithReuseIdentifier: while fetching collection view cells.

这是我的代码:

这个工作正常:

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

虽然编译得很好但不能正常工作(我没有描述我在单元格上执行的更改)

While this compiles well, but not working (the changes I perform on cell is not depicted)

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

有什么想法吗?

推荐答案

这两个基本上是两种截然不同的方法。

These two are basically two very different methods.


  1. dequeReusableCellWithReuseIdentifier
    假设您有要查看的文章列表。假设你有50篇文章。屏幕不会同时在屏幕上显示所有50篇文章。它将根据您给行的高度一次显示有限的单元格。让我们说屏幕一次只显示5篇文章,现在你位于列表的顶部。该列表将显示1-5项。现在滚动时,为了显示第6个项目,列表会重新使用第1个单元格,将其配置为第6个单元格并显示它。到这时你的第一个单元格不在视图中。

2. cellForRowAtIndexPath
另一方面, cellForRowAtIndexPath 返回已在视图中的单元格或您提供的IndexPath。在这种情况下,如果单元格已经在内存中,它将返回该单元格,或者它将配置一个新单元格并返回。

2.cellForRowAtIndexPath : On the other hand cellForRowAtIndexPath returns you the cell which is already in the view or from IndexPath you provide. In this case if the cell is already in the memory, it will just return that or it will configure a new cell and return.

以下示例适用于UITableViews,但是UICollectionViews可以用同样的方式处理。

The example below is for UITableViews, but UICollectionViews can be handled in the same way.

- (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;

}

如果你还不清楚,请告诉我。

Let me know if you were still unclear.

这篇关于dequeueReusableCellWithReuseIdentifier:和cellForItemAtIndexPath之间的区别:的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆