У меня есть UICollectionView
. Когда пользователь нажимает на ячейку, цвет ячейки меняется на красный. Я закодировал вышеуказанную часть, и она работает отлично.CollectionView Основные моменты Неправильная ячейка при прокрутке
Однако, когда я прокручиваю ниже, другие ячейки в виде коллекции подсвечиваются красным цветом. Я уверен, что он не был выбран, а только выделен.
Как я могу это решить.
Мой код выглядит следующим образом:
cellForItemAtIndexPath Метод
-(UICollectionViewCell *)collectionView:(UICollectionView *)collectionView cellForItemAtIndexPath:(NSIndexPath *)indexPath{
static NSString *CellIdentifier = @"cell";
ViewCell *vc = [collectionView dequeueReusableCellWithReuseIdentifier:CellIdentifier forIndexPath:indexPath];
vc.vciv.image = [arrayOfImages objectAtIndex:indexPath.row];
return vc;
}
didSelectItemAtIndexPath Метод
-(void)collectionView:(UICollectionView *)collectionView didSelectItemAtIndexPath:(NSIndexPath *)indexPath {
if([items containsObject:allItem[indexPath.row]]){
UICollectionViewCell *c =[collectionView cellForItemAtIndexPath:indexPath];
c.backgroundColor = [UIColor clearColor];
} else {
UICollectionViewCell *c =[collectionView cellForItemAtIndexPath:indexPath];
c.backgroundColor = [UIColor redColor];
}
Вы должны следить за выбранный индекс и убедитесь, что вы установите цвет фона ячейки на красном или ясно 'cellForItemAtIndexPath' – Paulw11