2013-06-20 13 views
8

Czy istnieje możliwość zmiany koloru tła UICollectionView tylko podczas stuknięcia elementu. Próbowałem:Komórka UICollectionView zmienia tło podczas stuknięcia

-(void) collectionView:(UICollectionView *)collectionView didHighlightItemAtIndexPath:(NSIndexPath *)indexPath{ 
    //change color when tapped 
} 

-(void)collectionView:(UICollectionView *)collectionView didUnhighlightItemAtIndexPath:(NSIndexPath *)indexPath{ 
    //change back on touch up 
} 

Ale rezultatem jest to, że widzę zmianę tylko wtedy, gdy trzymam palec na dłużej. Czy jest kilka podobnych rzeczy, takich jak w UITableViewCell metoda willSelectItemAtIndexPath:?

Odpowiedz

28

Ale wynik jest to, że mogę zobaczyć zmiany tylko wtedy, gdy trzymam palec na trochę dłuższy czas

Opóźnienie swój przeżywają jest prawdopodobnie związane z „Opóźnienie treści akcentami” pole w scenorys.

the checkbox in storyboard

Spróbuj odznacz je.

8

Myślę, że możesz chcieć zachować wybraną komórkę z innym kolorem tła, prawda? Następnie spróbuj tego kodu.

- (void)collectionView:(UICollectionView *)collectionView didSelectItemAtIndexPath:(NSIndexPath *)indexPath 
{ 
    UICollectionViewCell *cell = [collectionView cellForItemAtIndexPath:indexPath]; 
    cell.backgroundColor = [UIColor magentaColor]; 
} 

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

Po prostu przypisz inny kolor BG dla komórek o różnym statusie. Dodatkowo poniższy kod jest dokumentacją metod wyzwalania sekwencji, podczas gdy ktoś dotyka komórki collectionView. Dokumenty te można również znaleźć w pliku UICollectionView.h, części protokołu UICollectionViewDelegate.

// Methods for notification of selection/deselection and highlight/unhighlight events. 
// The sequence of calls leading to selection from a user touch is: 
// 
// (when the touch begins) 
// 1. -collectionView:shouldHighlightItemAtIndexPath: 
// 2. -collectionView:didHighlightItemAtIndexPath: 
// 
// (when the touch lifts) 
// 3. -collectionView:shouldSelectItemAtIndexPath: or -collectionView:shouldDeselectItemAtIndexPath: 
// 4. -collectionView:didSelectItemAtIndexPath: or -collectionView:didDeselectItemAtIndexPath: 
// 5. -collectionView:didUnhighlightItemAtIndexPath: 

+0

szukałem tej odpowiedzi ... I zmieniał niestandardową siatkę do UICollectionView i Tableview skojarzonej widzenia. Implementacja obu metod pozwala na wybór, a następnie zmianę selekcji. Dzięki Steve, to mi się udało. –

3
// In Swift  
func collectionView(collectionView: UICollectionView, didSelectItemAtIndexPath indexPath: NSIndexPath) { 
    let cell = collectionView.cellForItemAtIndexPath(indexPath) as! UICollectionViewCell 
    cell.backgroundColor = UIColor.magentaColor() 
} 

func collectionView(collectionView: UICollectionView, didDeselectItemAtIndexPath indexPath: NSIndexPath) { 
    let cell = collectionView.cellForItemAtIndexPath(indexPath) as! UICollectionViewCell 
    cell.backgroundColor = UIColor.cyanColor() 
} 
+4

Ta metoda nie działa po przewinięciu widoku kolekcji. Ponownie wykorzystuje się również komórki ponownie wykorzystane. –

+0

Należy używać 'didHighlightItemAtIndexPath' i' didUnhighlightItemAtIndexPath' jak w [tej odpowiedzi] (http://stackoverflow.com/a/34503118/3681880) i powyższego pytania. – Suragch

Powiązane problemy