如果select了单元格,则不调用UICollectionView – didDeselectItemAtIndexPath
我做的第一件事就是设置选中的单元格。
- (UICollectionViewCell *)collectionView:(UICollectionView *)collectionView cellForItemAtIndexPath:(NSIndexPath *)indexPath { UICollectionViewCell *cell = [collectionView dequeueReusableCellWithReuseIdentifier:@"Cell" forIndexPath:indexPath]; cell.selected = YES; return cell; }
细胞成功select。 如果用户触摸选定的单元格,则应取消select单元格并调用代表。 但是这绝不会发生。
- (void)collectionView:(UICollectionView *)collectionView didSelectItemAtIndexPath:(NSIndexPath *)indexPath { NSLog(@"%s", __PRETTY_FUNCTION__); } - (void)collectionView:(UICollectionView *)collectionView didDeselectItemAtIndexPath:(NSIndexPath *)indexPath { NSLog(@"%s", __PRETTY_FUNCTION__); }
我知道,如果我以编程方式设置select,则不会调用委托。 代表和数据源被设置。
但是,这个代表被调用:
- (BOOL)collectionView:(UICollectionView *)collectionView shouldHighlightItemAtIndexPath:(NSIndexPath *)indexPath { NSLog(@"%s", __PRETTY_FUNCTION__); return YES; }
如果我删除cell.selected = YES
比一切工作。 有没有人可以解释这种行为?
问题是,该单元格被选中,但UICollectionView
不知道任何关于它。 对UICollectionView
的额外调用解决了这个问题:
[collectionView selectItemAtIndexPath:indexPath animated:NO scrollPosition:UICollectionViewScrollPositionNone];
完整代码:
- (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; }
我保留这个问题来帮助可能面临同样问题的人。
我认为解决scheme@zeiteisen提供的解决scheme。 实际的事情在于select模式。 没有任何设置的属性单元cell.selected
。
UICollectionView
名称有两个属性: allowsMultipleSelection
和allowsSelection
。
allowsMultipleSelection
默认为NO
, allowsSelection
为YES
。
如果你只想select一个单元格,那么初始化代码看起来就像
yourCollectionView.allowsMultipleSelection = NO; yourCollectionView.allowsSelection = YES; //this is set by default
然后,设置将允许您一次只select一个单元格,而不是更less。 您必须至lessselect一个单元格。 didDeselectItemAtIndexPath
不会被调用,除非您select另一个单元格。 点击已经select的UICollectionViewCell
将不会使其取消select。 这是实施背后的政策。
如果你想select多个单元格,那么初始化的代码应该如下所示:
yourCollectionView.allowsMultipleSelection = YES; yourCollectionView.allowsSelection = YES; //this is set by default
然后设置将允许select多个单元格。 此设置允许select无或多个UICollectionViewCell
。 选中的单元格数量是
0 <= number_selected_cell <= total_number_of_cell
这是多单元select背后的政策。
如果您打算使用上述任何一种,欢迎您使用苹果API,而无需额外的头痛,否则您必须使用您自己的代码或苹果的API来find解决scheme。
我遇到过同样的问题。 我预先select了表格加载单元格,但我不得不双击一个单元格取消select它。 @zeiteisen答案帮助了我。 不过,我在Swift 3工作,所以我想我会在Swift中给出答案。
func collectionView(_ collectionView: UICollectionView, cellForItemAt indexPath: IndexPath) -> UICollectionViewCell { let cell = collectionView.dequeueReusableCell(withReuseIdentifier: "Cell", for: indexPath) as! Cell if /*Test for Selection*/ { cell.isSelected = true collectionView.selectItem(at: indexPath, animated: false, scrollPosition: .left) } return cell }
将此行添加到您的didSelectItemAtIndexPath方法中
[collectionView selectItemAtIndexPath:indexPath animated:NO scrollPosition:nil]
重新加载单元是我的解决scheme。 我需要的是在短时间内改变单元格中的图像。 我将图像分配给cellForItemAtIndexPath中的imageView,当用户触摸单元格时,我更改图像并运行mycode,然后重新加载单元格。
func collectionView(collectionView: UICollectionView, didSelectItemAtIndexPath indexPath: NSIndexPath) { let cell = collectionView.cellForItemAtIndexPath(indexPath) as! MenuCollectionViewCell cell.backgroundImageView.image = UIImage(named: "selected") // handle tap events collectionView.reloadItemsAtIndexPaths([indexPath]) }
希望它可以帮助别人。