UICollectionView - 如果选择了单元格,则不会调用didDeselectItemAtIndexPath(UICollectionView - didDeselectItemAtInd
The first thing I do is to set the cell selected.
- (UICollectionViewCell *)collectionView:(UICollectionView *)collectionView cellForItemAtIndexPath:(NSIndexPath *)indexPath { UICollectionViewCell *cell = [collectionView dequeueReusableCellWithReuseIdentifier:@"Cell" forIndexPath:indexPath]; cell.selected = YES; return cell; }
And the cell is successfully selected. If the user touches the selected cell, than should the cell be deselected and the delegates be called. But this never happen.
- (void)collectionView:(UICollectionView *)collectionView didSelectItemAtIndexPath:(NSIndexPath *)indexPath { NSLog(@"%s", __PRETTY_FUNCTION__); } - (void)collectionView:(UICollectionView *)collectionView didDeselectItemAtIndexPath:(NSIndexPath *)indexPath { NSLog(@"%s", __PRETTY_FUNCTION__); }
I know that the delegates are not called if I set the selection programmatically. The delegate and datasource are set.
However, this delegate gets called:
- (BOOL)collectionView:(UICollectionView *)collectionView shouldHighlightItemAtIndexPath:(NSIndexPath *)indexPath { NSLog(@"%s", __PRETTY_FUNCTION__); return YES; }
If I remove the cell.selected = YES than everything is working. Is there any one who can me explain this behaviour?
解决方案
The issue is, that the cell is selected, but the UICollectionView does not know anything about it. An extra call for the UICollectionView solves the problem:
[collectionView selectItemAtIndexPath:indexPath animated:NO scrollPosition:UICollectionViewScrollPositionNone];
Full code:
- (UICollectionViewCell *)collectionView:(UICollectionView *)collectionView cellForItemAtIndexPath:(NSIndexPath *)indexPath { UICollectionViewCell *cell = [collectionView dequeueReusableCellWithReuseIdentifier:@"Cell" forIndexPath:indexPath]; cell.selected = YES; [collectionView selectItemAtIndexPath:indexPath animated:NO scrollPosition:UICollectionViewScrollPositionNone]; return cell; }
I keep the question to help someone who may face the same problem.
我要做的第一件事是设置选定的单元格。
- (UICollectionViewCell *)collectionView:(UICollectionView *) collectionView cellForItemAtIndexPath:(NSIndexPath *)indexPath { UICollectionViewCell * cell = [collectionView dequeueReusableCellWithReuseIdentifier:@CellforIndexPath:indexPath]; cell.selected = YES; 返回单元格; }
并且成功选择了单元格。
如果用户触摸所选单元格,则应取消选择单元格并调用委托。但这绝不会发生。
- (void)collectionView:(UICollectionView *)collectionView didSelectItemAtIndexPath:(NSIndexPath *)indexPath { NSLog(@) %s,__ PRETTY_FUNCTION__); } - (void)collectionView:(UICollectionView *)collectionView didDeselectItemAtIndexPath:(NSIndexPath *)indexPath { NSLog(@%s,__ PRETTY_FUNCTION__); }
我知道如果我以编程方式设置选择,则不会调用委托。
设置了委托和数据源。
然而,这个委托被调用:
- (BOOL)collectionView:(UICollectionView *)collectionView shouldHighlightItemAtIndexPath:(NSIndexPath *)indexPath { NSLog(@%s,__ PRETTY_FUNCTION__); 返回YES; }
如果我删除 cell.selected = YES 比一切正常。有没有人可以解释这种行为?
解决方案
问题是,单元格已被选中,但 UICollectionView 对此一无所知。额外调用 UICollectionView 解决了这个问题:
[collectionView selectItemAtIndexPath :indexPath animated:NO scrollPosition:UICollectionViewScrollPositionNone];
完整代码:
- (UICollectionViewCell *)collectionView:(UICollectionView *)collectionView cellForItemAtIndexPath:(NSIndexPath *)indexPath { UICollectionViewCell * cell = [collectionView dequeueReusableCellWithReuseIdentifier:@CellforIndexPath:indexPath]; cell.selected = YES; [collectionView selectItemAtIndexPath:indexPath animated:NO scrollPosition:UICollectionViewScrollPositionNone]; 返回单元格; }
我保留问题以帮助可能遇到同样问题的人。