2013-10-24 25 views
5

Buduję aplikację w iOS i chcę, aby komórki w moim CollectionView podświetliły po dotknięciu, podobnie jak zwykłe przyciski. Jak mogę to osiągnąć w metodzie didSelectItemAtIndexPath: (NSIndexPath *) indexPath?Zaznaczanie komórek w CollectionView

Dzięki

+0

Zmień kolor tła tej komórki. – user1673099

Odpowiedz

7

spróbować czegoś takiego:

- (UICollectionViewCell *)collectionView:(UICollectionView *)collectionView cellForItemAtIndexPath:(NSIndexPath *)indexPath{ 
    ..... 
    if (cell.selected) { 
     cell.backgroundColor = [UIColor colorWithRed:255/255.0 green:255/255.0 blue:153/255.0 alpha:1]; // highlight selection 
    } 
    else 
    { 
     cell.backgroundColor = [UIColor clearColor]; // Default color 
    } 
    return cell; 
} 

- (void)collectionView:(UICollectionView *)collectionView didSelectItemAtIndexPath:(NSIndexPath *)indexPath{ 
    UICollectionViewCell* cell = [collectionView cellForItemAtIndexPath:indexPath]; 
    cell.backgroundColor = [UIColor colorWithRed:255/255.0 green:255/255.0 blue:153/255.0 alpha:1]; //  //cell.lblImgTitle.text = @"xxx"; 
} 

- (void)collectionView:(UICollectionView *)collectionView didDeselectItemAtIndexPath:(NSIndexPath *)indexPath{ 
    UICollectionViewCell* cell = [collectionView cellForItemAtIndexPath:indexPath]; 
    cell.backgroundColor = [UIColor clearColor]; 
} 
+0

Rozwiązałem go, zmieniając Alpha obrazu w komórce w metodach didHighlightItemAtIndexPath i didUnhighlightItemAtIndexPath. W każdym razie ustawię twoją odpowiedź jako właściwą odpowiedź. Dzięki. –

+1

dlaczego nie piszesz, jak rozwiązałeś swój problem w odpowiedzi? Będzie to pomocne dla innych ... diogo-appdev –

0

spróbować tej

cell.selectedBackgroundView.backgroundColor = [UIColor greenColor]; 
5

Jeśli podklasy klasy komórek, umieścić to w pliku .m

- (void)setSelected:(BOOL)selected 
{ 
    if(selected) 
    { 
     self.backgroundColor = [UIColor colorWithWhite:0.1 alpha:0.5]; 
    } 
    else 
    { 
     self.backgroundColor = [UIColor whiteColor]; 
    } 
} 
0

dlaczego nie można zmienić kolor tła przez strąki kakao? I nowy zdefiniowany przez użytkownika collectionView komórka klasy `

- (void)collectionView:(UICollectionView *)collectionView didSelectItemAtIndexPath:(NSIndexPath *)indexPath{ 
     CHSMenuControlCell *cell = (CHSMenuControlCell*)[collectionView cellForItemAtIndexPath:indexPath]; 
     cell.backgroundColor = [UIColor colorWithRed:255/255.0 green:255/255.0 blue:153/255.0 alpha:1]; //  //cell.lblImgTitle.text = @"xxx"; 
    } 

    - (void)collectionView:(UICollectionView *)collectionView didDeselectItemAtIndexPath:(NSIndexPath *)indexPath{ 
    CHSMenuControlCell *cell = (CHSMenuControlCell *)[collectionView cellForItemAtIndexPath:indexPath]; 
    cell.backgroundColor = [UIColor clearColor]; 
}