1

Я хочу разрешить пользователю выбирать только одну ячейку & данные будут отражаться на этом основании. (поэтому одновременно будет выбрана только одна ячейка.), но вместо этого, если я быстро нажимаю несколько пальцев, я могу выбрать несколько ячеек в определенное время.Collectionview single selection

Я сделал это с didSelectItemAtIndexPath & didDeselectItemAtIndexPath также, но в этом случае я не могу выбрать.

Может ли кто-нибудь помочь в этой функции?

Вот пример кода, который я использую: -

[CustomCollection setShowsHorizontalScrollIndicator:NO]; 
    [CustomCollection setBounces:NO]; 
    [CustomCollection setAllowsMultipleSelection:NO]; 
    [CustomCollection setMultipleTouchEnabled:NO]; 

-(UICollectionViewCell *)collectionView:(UICollectionView *)collectionView cellForItemAtIndexPath:(NSIndexPath *)indexPath 
{ 
    static NSString *identifier = @"TheCustomCell"; 

     CustomCell *cell = [collectionView dequeueReusableCellWithReuseIdentifier:identifier forIndexPath:indexPath]; 

     if (previousIndexPath.row == indexPath.row) { 
    // Code for Selecttion 
     [cell.tickImgView setImage:[UIImage imageNamed:@"Select_tick"]]; 

    } 
    else{ 
     [cell.tickImgView setImage:[UIImage imageNamed:@"Deselect_tick"]]; 

} 


} 


-(void)collectionView:(UICollectionView *)collectionView didSelectItemAtIndexPath:(NSIndexPath *)indexPath 
{ 
    dispatch_async(dispatch_get_main_queue(), ^{ 
     previousIndexPath = indexPath; 
     [CustomCollection reloadData]; 
    }); 
} 
+0

http://stackoverflow.com/questions/34614815/how-to-disable-multiple-touch-on-a-uicollectionview –

+0

@ManishPathak Я пробовал этот способ, но он все еще выбирает несколько ячеек. –

+0

Требуется перезагрузка коллекцииView при каждом выборе элемента. –

ответ

0

И наконец, я нашел решение.

Я только сделал мой собственный didDeselect метод & ручка в cellForItemAtIndexPath.

1

Ниже мой рабочий код.

Я объявил previousIndexPath в .h как Follo ниже

@property (nonatomic, strong) NSIndexPath *previousIndexPath; 

Теперь в .m файл

-(UICollectionViewCell *)collectionView:(UICollectionView *)collectionView cellForItemAtIndexPath:(NSIndexPath *)indexPath 
{ 
    static NSString *identifier = @"TheCustomCell"; 

    CustomCell *cell = [collectionView dequeueReusableCellWithReuseIdentifier:identifier forIndexPath:indexPath]; 

    if ([self.previousIndexPath isEqual:indexPath]) { 
     // Code for Selecttion 
     [cell.tickImgView setImage:[UIImage imageNamed:@"Select_tick"]]; 

    } 
    else{ 
     //Code for deselect 
     [cell.tickImgView setImage:[UIImage imageNamed:@"Deselect_tick"]]; 

    } 
    return cell; 

} 


-(void)collectionView:(UICollectionView *)collectionView didSelectItemAtIndexPath:(NSIndexPath *)indexPath 
{ 
    //reload collectionview data only if its not selected already 
    if (![self.previousIndexPath isEqual:indexPath]) 
    { 
     self.previousIndexPath = indexPath; 
     [collectionView reloadData]; 

    } 

} 

Я сравнил indexpath непосредственно с помощью isEqual: метод вместо indexPath.row и == операторов.

+0

Вы также удалили вызов 'dispatch_async()'. – Caleb

+0

@ Калеб прав, и он по-прежнему входит в состояние If для cellForItemAtIndexPath и множественного выбора. –