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

查看:104
本文介绍了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天全站免登陆