UICollectionView - didDeselectItemAtIndexPath not called if cell is selected

后端 未结 7 1601
面向向阳花
面向向阳花 2020-12-01 09:12

The first thing I do is to set the cell selected.

- (UICollectionViewCell *)collectionView:(UICollectionView *)collectionView cellForItemAtIndexPath:(NSIndex         


        
7条回答
  •  渐次进展
    2020-12-01 09:39

    I think the solution @zeiteisen provided is a way around solution. The actual thing lies in the selection mode. There is nothing to be set the in property cell.selected.

    There are two properties of the UICollectionView name allowsMultipleSelection and allowsSelection.

    allowsMultipleSelection is NO by default and allowsSelection is YES.

    If you want to select only one cell then the code @the initialization look like

    yourCollectionView.allowsMultipleSelection = NO;
    yourCollectionView.allowsSelection = YES; //this is set by default
    

    Then the settings will allow you select only one cell at a time, not less not more. You must have to select at least one cell. The didDeselectItemAtIndexPath wont be called unless you select another cell. Tapping an already selected UICollectionViewCell won't make it to Deselect. This is the policy behind the implementation.

    If you want to select multiple cell then the code @the initialization should look like the following:

    yourCollectionView.allowsMultipleSelection = YES;
    yourCollectionView.allowsSelection = YES; //this is set by default
    

    Then the settings will allow to select multiple cell. This settings allow none or multiple UICollectionViewCell to be selected. Number of cell selected will be

    0 <= number_selected_cell <= total_number_of_cell
    

    This is the policy behind the multiple cell selection.

    If you are intended to use any of the above you are welcome to use apples api without extra headache, otherwise you got to find a way around to sneak into your goal using your own code or apple's api.

提交回复
热议问题