回答

1

您可以使用您的模型對象來保存其中的選定屬性(或者您可以爲此目的創建一個布爾數組)。並在shouldSelectItemAtIndexPath方法中檢查它。

@cihangirs代碼:

- (BOOL)collectionView:(UICollectionView *)collectionView shouldSelectItemAtIndexPath:(NSIndexPath *)indexPath { 
    if (someModel.isSelected) { 
     return NO; 
    } else { 
     someModel.isSelected = YES; 
     return YES; 
    } 
} 
0

這是做你的目標最安全的辦法: -

(void)collectionView:(UICollectionView *)collectionView 
didSelectItemAtIndexPath:(NSIndexPath *)indexPath 
{ 
    if([[collectionView indexPathsForSelectedItems] containsObject:indexPath]) // checking whether cell is already selected or not 
    { 
     return; 
    } 
else 
    { 
// do whatever you want to do on selection of cell 

    } 
} 

的事情在這裏發生的事情,當你選擇一個單元格,它會自動獲取存儲收集的意見「indexPathsForSelectedItems」,所以下次你再次敲擊選中的單元格,此方法[[collectionView indexPathsForSelectedItems] containsObject:indexPath]將檢查該單元格是否已被選中,如果是,則它將返回該方法,以使其不再進一步。