我决定使用drawRect:
而不是自定义 xib 文件来绘制我的 UICollectionViewCell 以提高性能(因为我有阴影和标签阴影)。
出于某种原因,因为我改变了这一点,我的单元格在每次重新加载到UICollectionView
.
当我记录它设置为加载的图像名称时collectionView:cellForItemAtIndexPath
,drawRect:
它提供了正确的图像名称和正确的索引路径,但是当我加载应用程序时,它会在滚动时混淆它们,即使最初,它们已正确加载。
这是我的代码:
viewDidLoad
// Collection cells
//
[self.collectionView registerClass:[CluelessSymbolCell class] forCellWithReuseIdentifier:@"Symbol cell"];
集合视图:cellForItemAtIndexPath
- (UICollectionViewCell *)collectionView:(UICollectionView *)collectionView cellForItemAtIndexPath:(NSIndexPath *)indexPath
{
CluelessSymbolCell *cell = (CluelessSymbolCell *)[collectionView dequeueReusableCellWithReuseIdentifier:@"Symbol cell" forIndexPath:indexPath];
CluelessClue *clue = [self.clues objectAtIndex:indexPath.row];
NSLog(@"(%@) Image: %@", indexPath, [clue.imageNames objectAtIndex:0]);
[cell setClue:clue];
return cell;
}
drawRect:(在单元格子类'CluelessSymbolCell'中)
- (void)drawRect:(CGRect)rect
{
NSString *imageName = [NSString stringWithFormat:@"%@-icon-iphone", [self.clue.imageNames objectAtIndex:0]];
// Cell
//
[self.contentView.layer setShadowColor:[UIColor blackColor].CGColor];
[self.contentView.layer setShadowOpacity:0.3f];
[self.contentView.layer setShadowRadius:2.0f];
[self.contentView.layer setShadowOffset:CGSizeZero];
[self.contentView.layer setShadowPath:[UIBezierPath bezierPathWithRect:self.layer.bounds].CGPath];
// Image
//
CALayer *imageLayer = [CALayer layer];
[imageLayer setFrame:rect];
[imageLayer setContents:(id)[[UIImage imageNamed:imageName] CGImage]];
[self.contentView.layer addSublayer:imageLayer];
}
编辑
在记录单元格内容视图的子层时,我可以看到每个单元格的内容视图子层已移动到下一个单元格,这是什么原因以及如何克服它?
日志:
2013-02-22 23:41:08.064 Clueless[3462:c07] <NSIndexPath 0x845cb90> 2 indexes [0, 0], (null)
2013-02-22 23:41:08.065 Clueless[3462:c07] <NSIndexPath 0x84435f0> 2 indexes [0, 1], (null)
2013-02-22 23:41:08.066 Clueless[3462:c07] <NSIndexPath 0x8446f50> 2 indexes [0, 2], (null)
2013-02-22 23:41:08.066 Clueless[3462:c07] <NSIndexPath 0x843a060> 2 indexes [0, 3], (null)
2013-02-22 23:41:08.067 Clueless[3462:c07] <NSIndexPath 0x846bc70> 2 indexes [0, 4], (null)
2013-02-22 23:41:08.067 Clueless[3462:c07] <NSIndexPath 0x845cd20> 2 indexes [0, 5], (null)
2013-02-22 23:41:08.068 Clueless[3462:c07] <NSIndexPath 0x845ce60> 2 indexes [0, 6], (null)
2013-02-22 23:41:08.068 Clueless[3462:c07] <NSIndexPath 0x846ce40> 2 indexes [0, 7], (null)
2013-02-22 23:41:08.069 Clueless[3462:c07] <NSIndexPath 0x846c9b0> 2 indexes [0, 8], (null)
2013-02-22 23:41:08.069 Clueless[3462:c07] <NSIndexPath 0x846d880> 2 indexes [0, 9], (null)
2013-02-22 23:41:08.070 Clueless[3462:c07] <NSIndexPath 0x846db20> 2 indexes [0, 10], (null)
2013-02-22 23:41:08.071 Clueless[3462:c07] <NSIndexPath 0x846df30> 2 indexes [0, 11], (null)
2013-02-22 23:41:15.105 Clueless[3462:c07] <NSIndexPath 0x846ec60> 2 indexes [0, 12], (null)
2013-02-22 23:41:15.106 Clueless[3462:c07] <NSIndexPath 0x846efb0> 2 indexes [0, 13], (null)
2013-02-22 23:41:15.107 Clueless[3462:c07] <NSIndexPath 0x846f6d0> 2 indexes [0, 14], (null)
2013-02-22 23:41:15.753 Clueless[3462:c07] <NSIndexPath 0x82492e0> 2 indexes [0, 15], (
"<CALayer: 0x8495840>"
)
2013-02-22 23:41:15.754 Clueless[3462:c07] <NSIndexPath 0x827f750> 2 indexes [0, 16], (
"<CALayer: 0x8491010>"
)
2013-02-22 23:41:15.754 Clueless[3462:c07] <NSIndexPath 0x827f5c0> 2 indexes [0, 17], (
"<CALayer: 0x8493ef0>"
)
2013-02-22 23:41:15.986 Clueless[3462:c07] <NSIndexPath 0x845cb90> 2 indexes [0, 0], (
"<CALayer: 0x8493ef0>"
)
2013-02-22 23:41:15.987 Clueless[3462:c07] <NSIndexPath 0x84435f0> 2 indexes [0, 1], (
"<CALayer: 0x8491010>"
)
2013-02-22 23:41:15.987 Clueless[3462:c07] <NSIndexPath 0x8446f50> 2 indexes [0, 2], (
"<CALayer: 0x8495840>"
)
2013-02-22 23:41:26.161 Clueless[3462:c07] <NSIndexPath 0x827d970> 2 indexes [0, 12], (
"<CALayer: 0x8241e20>"
)
2013-02-22 23:41:26.162 Clueless[3462:c07] <NSIndexPath 0x824e9d0> 2 indexes [0, 13], (
"<CALayer: 0x827bc60>"
)
2013-02-22 23:41:26.163 Clueless[3462:c07] <NSIndexPath 0x827d030> 2 indexes [0, 14], (
"<CALayer: 0x8242fb0>"
)
2013-02-22 23:41:26.776 Clueless[3462:c07] <NSIndexPath 0x82492e0> 2 indexes [0, 15], (
"<CALayer: 0x8491010>"
)
2013-02-22 23:41:26.777 Clueless[3462:c07] <NSIndexPath 0x827f750> 2 indexes [0, 16], (
"<CALayer: 0x8493ef0>"
)
2013-02-22 23:41:26.778 Clueless[3462:c07] <NSIndexPath 0x827f5c0> 2 indexes [0, 17], (
"<CALayer: 0x8495840>"
)
2013-02-22 23:41:26.926 Clueless[3462:c07] <NSIndexPath 0x845cb90> 2 indexes [0, 0], (
"<CALayer: 0x8495840>"
)
2013-02-22 23:41:26.927 Clueless[3462:c07] <NSIndexPath 0x84435f0> 2 indexes [0, 1], (
"<CALayer: 0x8493ef0>"
)
2013-02-22 23:41:26.928 Clueless[3462:c07] <NSIndexPath 0x8446f50> 2 indexes [0, 2], (
"<CALayer: 0x8491010>"
)