Pdf UICollectionView在滚动后显示错误的单元格-出列问题?
我在UIViewController中有一个UICollectionView。在collectionView cellForItemAtIndexPath:方法中,它基于数据源创建一系列自定义单元格。自定义单元格依次包含一个UIView,子类化用于绘制单个PDF页面 它的设置方式是将PDF文件拆分为单个页面,因此单元格1包含PDF第1页,单元格2包含PDF第2页,依此类推。到目前为止还不错,我的问题是: 向下滚动时,UICollectionView开始显示错误的单元格。例如,在一个34页的文档中,它以正确的顺序显示单元格/第1-16页,但随后开始显示似乎已进一步排队的页面,例如单元格1、单元格2、单元格4。我从来没有接近过手机/第34页 我以前在UITableView中见过类似的行为,我相信这与单元格的出列或委托方法有关。不太确定-感谢您的帮助Pdf UICollectionView在滚动后显示错误的单元格-出列问题?,pdf,uicollectionview,uicollectionviewcell,Pdf,Uicollectionview,Uicollectionviewcell,我在UIViewController中有一个UICollectionView。在collectionView cellForItemAtIndexPath:方法中,它基于数据源创建一系列自定义单元格。自定义单元格依次包含一个UIView,子类化用于绘制单个PDF页面 它的设置方式是将PDF文件拆分为单个页面,因此单元格1包含PDF第1页,单元格2包含PDF第2页,依此类推。到目前为止还不错,我的问题是: 向下滚动时,UICollectionView开始显示错误的单元格。例如,在一个34页的文档中
- (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; }
我也有类似的问题。这很可能是因为重复使用的单元格不会重新绘制自己。在自定义单元格的内容类(PDF视图)中,如果框架更新,则触发重画:
-(void)setFrame:(CGRect)frame {
[super setFrame:frame];
[self setNeedsDisplay]; // force drawRect:
}
这对我有用。此外,如果您的单元格大小可能会更改,请设置自动调整大小掩码,使其用
self.autoresizingMask = UIViewAutoresizingFlexibleWidth | UIViewAutoresizingFlexibleHeight;
初始化期间。使用PrepareForeUse方法修复了类似问题 只需将此方法添加到自定义单元格实现中
- (void)prepareForReuse {
self.fileName = nil;
self.title = nil;
// add remaining properties
}
根据Asatur Galstyan的回答,我在swift中修复了一个类似的问题 将自定义类关联到情节提要中的单元格后,可以重写prepareForReuse()函数:
import UIKit
class SomeCollectionViewCell: UICollectionViewCell {
@IBOutlet weak var exampleView: UIView!
@IBOutlet weak var exampleLabel: UILabel!
override func prepareForReuse(){
super.prepareForReuse()
exampleLabel.textColor = nil
exampleView.backgroundColor = nil
exampleView.layer.cornerRadius = 0
}
}
prepareForReuse()的默认实现没有任何功能(至少在iOS 10中是这样),但是 也许我也有同样的问题。你找到解决办法了吗?这对我很有效-谢谢你的帮助。从UICollectionView的cellForItemAtIndexPath内部调用上面的setFrame方法,强制PDF重新绘制。非常感谢,太好了!调用
setFrame:
您自己可能没有必要,因为集合视图无论如何都会这样做。设置大小掩码很有帮助,我遵循了Chris的建议,尽管我实际上需要在设置单元格后和返回单元格之前调用单元格上的setNeedsDisplay
。@PuneethKamath您不需要调用此方法,只需将此添加到UICollectionViewCell子类中。根据您的示例,我发现exampleView.setNeedsDisplay()
中的prepareforeuse()
解决了这个问题。