我有一个集合查看&我要选择一个以上的项目。 对于我使用
- (void)collectionView:(UICollectionView *)collectionView didSelectItemAtIndexPath:(NSIndexPath *)indexPath
{
[self.selectedAsset addObject:self.assets[indexPath.row]];
UICollectionViewCell* cell=[self.collectionView cellForItemAtIndexPath:indexPath];
cell.contentView.backgroundColor = [UIColor blackColor];
}
这种方法将对象添加到selectedAsset
的NSMutableArray。
这是cellForItemAtIndexPath
方法。
- (UICollectionViewCell *)collectionView:(UICollectionView *)cv cellForItemAtIndexPath:(NSIndexPath *)indexPath;
{
Cell *cell = [cv dequeueReusableCellWithReuseIdentifier:@"MY_CELL" forIndexPath:indexPath];
// load the asset for this cell
ALAsset *asset = self.assets[indexPath.row];
CGImageRef thumbnailImageRef = [asset thumbnail];
UIImage *thumbnail = [UIImage imageWithCGImage:thumbnailImageRef];
// apply the image to the cell
cell.imageView.image = thumbnail;
[cell.label removeFromSuperview];
//cell.imageView.contentMode = UIViewContentModeScaleToFill;
return cell;
}
我使用此代码的机会单元格的背景颜色。
UICollectionViewCell* cell=[self.collectionView cellForItemAtIndexPath:indexPath];
cell.contentView.backgroundColor = [UIColor blackColor];
但是,当我在集合视图中的集合视图中选择第一个项目,无论是第1和第15项改变背景颜色。
为什么出现这种情况? 请能有人给我一个解决方案。
好吧,这里似乎是你的问题。
1)当你选择第一个单元格这种方法被称为
- (void)collectionView:(UICollectionView *)collectionView didSelectItemAtIndexPath:(NSIndexPath *)indexPath
在这种方法中,您更改单元格背景色是黑色,所以这种细胞是黑从现在开始。
2)你向下滚动,新的细胞装载方法
-(UICollectionViewCell *)collectionView:(UICollectionView *)cv cellForItemAtIndexPath:(NSIndexPath *)indexPath;
有在执行一个棘手线
dequeueReusableCellWithReuseIdentifier:
因此,对于新的电池你的应用程序将可能无法创建新的细胞,而且还显示出一个不可见,例如小区#1,你在开始时选择,并得到了黑色的背景色。
因此,对于新的电池你的应用程序可能会重新使用旧的一个可能被修改。
我的修为,你会是下一个 -
- (UICollectionViewCell *)collectionView:(UICollectionView *)cv cellForItemAtIndexPath:(NSIndexPath *)indexPath;
{
UICollectionViewCell *cell = [cv dequeueReusableCellWithReuseIdentifier:@"MY_CELL" forIndexPath:indexPath];
//line below might not work, you have to tune it for your logic, this BOOL needs to return weather cell with indexPath is selected or not
BOOL isCellSelected = [self.selectedAsset containsObject:self.assets[indexPath.row]];
if(!isCellSelected)
{
UICollectionViewCell* cell=[cv cellForItemAtIndexPath:indexPath];
cell.contentView.backgroundColor = [UIColor clearColor]; // or whatever is default for your cells
}
// load the asset for this cell
ALAsset *asset = self.assets[indexPath.row];
CGImageRef thumbnailImageRef = [asset thumbnail];
UIImage *thumbnail = [UIImage imageWithCGImage:thumbnailImageRef];
// apply the image to the cell
cell.imageView.image = thumbnail;
[cell.label removeFromSuperview];
//cell.imageView.contentMode = UIViewContentModeScaleToFill;
return cell;
}