Warning: file_get_contents(/data/phpspider/zhask/data//catemap/9/ios/107.json): failed to open stream: No such file or directory in /data/phpspider/zhask/libs/function.php on line 167

Warning: Invalid argument supplied for foreach() in /data/phpspider/zhask/libs/tag.function.php on line 1116

Notice: Undefined index: in /data/phpspider/zhask/libs/function.php on line 180

Warning: array_chunk() expects parameter 1 to be array, null given in /data/phpspider/zhask/libs/function.php on line 181
Ios cellForItemAtIndexPath中的单元格不唯一:_Ios_Cocoa Touch_Uicollectionview_Uicollectionviewcell - Fatal编程技术网

Ios cellForItemAtIndexPath中的单元格不唯一:

Ios cellForItemAtIndexPath中的单元格不唯一:,ios,cocoa-touch,uicollectionview,uicollectionviewcell,Ios,Cocoa Touch,Uicollectionview,Uicollectionviewcell,这些线路上到底发生了什么 - (UICollectionViewCell *)collectionView:(UICollectionView *)collectionView cellForItemAtIndexPath:(NSIndexPath *)indexPath { MenuCollectionViewCell *cell = (MenuCollectionViewCell *)[collectionView dequeueReusableCellWithReuseIdentif

这些线路上到底发生了什么

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

  MenuCollectionViewCell *cell = (MenuCollectionViewCell *)[collectionView dequeueReusableCellWithReuseIdentifier:reuseIdentifier
                                                                                                     forIndexPath:indexPath];
特别是在细胞系上

据我所知,当我滚动集合视图显示不可见的新单元格,并因此隐藏可见单元格时,iOS将重用屏幕外的单元格来绘制新单元格,对吗

问题在于:如果我基于indexPath定制了一个特定的单元格,那么我将多次重复该定制。例如,如果我将一个单元格的标签设置为红色,则在我滚动集合视图时,将有许多单元格带有红色标签,并且该单元格将退出队列并重新使用


如何防止这种情况发生?

覆盖
[UICollectionViewCell prepareForReuse]
并重置方法中的所有自定义设置。

覆盖
[UICollectionViewCell prepareForReuse]
并重置方法中的所有自定义设置。

您可以在
单元格中清除它们,然后单击“行路径”
,在为新的indexPath配置单元格之前,或者如果您有UITableViewCell的自定义子类,请重写
prepareForReuse
方法并将其清除。看

编辑
很抱歉,您使用的是集合视图而不是表视图,但同样适用。

您可以在为新indexPath配置单元格之前,在
单元格中清除它们,或者如果您有自定义的UITableViewCell子类,重写
prepareforeuse
方法并在下面清除它。看

编辑
很抱歉,您使用的是集合视图而不是表视图,但同样适用。

覆盖
prepareforeuse
要重置资源,当单元格滚动出帧时将调用它


不要忘记调用超级方法

覆盖
prepareforeuse
重置资源,当单元格滚动出帧时将调用它

别忘了调用超级方法