ios - dequeueReusableCellWithReuseIdentifier : and cellForItemAtIndexPath: 之间的区别

标签 ios objective-c uikit uicollectionview uicollectionreusableview

我一直想知道为什么我的代码在获取 Collection View 单元格时与 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..
        }

有什么想法吗?

最佳答案

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

  1. dequeReusableCellWithReuseIdentifier: 假设您有一个要查看的文章列表。假设您有 50 篇文章。屏幕不会一次显示所有 50 篇文章。它会根据您为行指定的高度一次显示有限的单元格。假设屏幕一次只显示 5 篇文章,而现在您位于列表的顶部。该列表将显示项目 1-5。现在,当您滚动时,为了显示第 6 个项目,列表会重复使用您的第一个单元格,将其配置为第 6 个单元格并显示它。这时候您的第一个单元格已经不在视野范围内了。

2.cellForRowAtIndexPath : 另一方面,cellForRowAtIndexPath 返回已在 View 中或来自您提供的 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;

}

如果您仍然不清楚,请告诉我。

关于ios - dequeueReusableCellWithReuseIdentifier : and cellForItemAtIndexPath: 之间的区别,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/23402263/

相关文章:

android - 粘性页脚导致内容低于首屏

ios - 呈现一个导航栏和一个非 Root View 选项卡 Controller

ios - 即使创建了 Swift Bridging Header 文件,也无法在 Objective C 中引用 Swift 类

ios - 如何解决我的 View 的自动布局约束

objective-c - 找不到符号 : _OBJC_CLASS_$_Article

ios - 在 iOS 上使用 UINavigationController 时如何在屏幕上垂直居中?

html - 将字体属性添加到 HTML 渲染的 UILabel swift

ios - NSOperation 在后台任务结束之前完成

uikit - 无法在 iOS7 中淡出 UIToolbar 或 UITabbar

ios - 锁定 UIView 相对于 UIScrollView 的位置