UICollectionView 在滚动后显示错误的单元格 - 出队问题? [英] UICollectionView showing wrong cells after scrolling - dequeue issue?

查看:26
本文介绍了UICollectionView 在滚动后显示错误的单元格 - 出队问题?的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我在 UIViewController 中有一个 UICollectionView.在 collectionView cellForItemAtIndexPath: 方法中,它根据数据源创建一系列自定义单元格.自定义单元格又包含一个 UIView,子类化以绘制单个 PDF 页面.

I have a UICollectionView within a UIViewController. In the collectionView cellForItemAtIndexPath: method, it creates a series of custom cells based on the datasource. The custom cells in turn contain a UIView, subclassed to draw single PDF pages.

它的设置方式是将 PDF 文件拆分为单个页面,因此单元格 1 包含 PDF 第 1 页,单元格 2 包含 PDF 第 2 页,依此类推.到目前为止一切顺利,这是我的问题:

It's set up in such a way as to split a PDF file into its single pages, so cell 1 contains PDF page 1, cell 2 contains PDF page 2 and so on. So far so good, here's my problem:

当我向下滚动时,UICollectionView 开始显示错误的单元格.例如,在一个 34 页的文档中,它以正确的顺序显示单元格/第 1-16 页,但随后开始显示 似乎 已进一步向上出列的页面,例如单元格 1、单元格 2、单元格 4.我从来没有靠近单元格/第 34 页.

When I scroll down, the UICollectionView starts displaying the wrong cells. For instance in a 34 page document, it shows cells/pages 1-16 in the correct order, but then starts displaying pages that seemed to have been dequeued further up, such as cell 1, cell 2, cell 4. I never get anywhere near cell/page 34.

我过去曾在 UITableView 中看到过类似的行为,并且认为这与单元格的出列或委托方法有关.不太确定 - 任何帮助表示赞赏.

I've seen similar behaviour from UITableView in the past, and believe it has something to do with the dequeueing of the cells, or a delegate method. Not quite sure - any help is appreciated.

- (UICollectionViewCell *)collectionView:(UICollectionView *)collectionView cellForItemAtIndexPath:(NSIndexPath *)indexPath {

//create custom cell
CustomCell *cell = [collectionView dequeueReusableCellWithReuseIdentifier:@"cellID" forIndexPath:indexPath];

//set file name (always the same; one PDF file)
cell.fileName = fileName;
cell.backgroundColor = [UIColor clearColor];

//set the title to the page number
cell.title = [NSString stringWithFormat:@"page %@", [countArray objectAtIndex:indexPath.row]];

//set the current page (which indicates which page to display) according to the pageCount
cell.currentPage = [[countArray objectAtIndex:indexPath.row] intValue];

return cell; }

推荐答案

我也遇到过类似的问题.这很可能是因为重用的单元格不会自己重绘.在您的自定义单元格的 content 类(您的 PDF 视图)中,如果框架更新,则触发重绘:

I've had similare issues. This is most likely because the reused cells do not redraw themselves. In your custom cell's content class (your PDF view), trigger redrawing if the frame is updated:

-(void)setFrame:(CGRect)frame {
    [super setFrame:frame];
    [self setNeedsDisplay]; // force drawRect:
}

这对我有用.此外,如果您的单元格大小可能会发生变化,请设置自动调整大小掩码,使其填充空间

This worked for me. In addition, if your cell size may change, set the autoresizing mask so that it fills space with

self.autoresizingMask = UIViewAutoresizingFlexibleWidth | UIViewAutoresizingFlexibleHeight;

在初始化期间.

这篇关于UICollectionView 在滚动后显示错误的单元格 - 出队问题?的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

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